Cómo mover / copiar archivos localmente con Chef


Todavía no he encontrado un recurso de Chef que copie/mueva archivos localmente. Por ejemplo, quiero descargar jetty hightide y descomprimirlo. Una vez hecho esto, copie todos los archivos en una carpeta en particular, así:

# mv /var/tmp/jetty-hightide-7.4.5.v20110725/* /opt/jetty/

POR cierto, jettyhightide cuando se descomprime, le da una carpeta y dentro de esa carpeta se encuentran el resto de los archivos. Por lo tanto unzip jetty-hightide-7.4.5.v20110725.zip -d /opt/jetty/ es inútil porque creará un directorio /opt/jetty/jetty-hightide-7.4.5.v20110725/* mientras que lo que realmente quiero es /opt/jetty/*. Por lo tanto, estoy buscando una copia local / mover recurso en Chef.

 47
Author: codeforester, 2013-10-07

8 answers

Cómo copiar un solo archivo

Primera vía

Utilizo la instrucción file para copiar el archivo (comprobación en tiempo de compilación)

file "/etc/init.d/someService" do
  owner 'root'
  group 'root'
  mode 0755
  content ::File.open("/home/someService").read
  action :create
end

Aquí:

  • "/etc/init.d/someService" - archivo de destino,
  • "/home/someService" - archivo fuente

También puede envolver ::File.open("/home/someService").read en lazy bloque

...
lazy { ::File.open("/home/someService").read }
...

Segunda vía

User remote_file statement (run-time check)

remote_file "Copy service file" do 
  path "/etc/init.d/someService" 
  source "file:///home/someService"
  owner 'root'
  group 'root'
  mode 0755
end

Tercera vía

También puedes usar shell / batch

Para cada directorio

Dir[ "/some/directory/resources/**/*" ].each do |curr_path|
  file "/some/target/dir/#{Pathname.new(curr_path).basename}" do
    owner 'root'
    group 'root'
    mode 0755
    content lazy { IO.read(curr_path, mode: 'rb').read }
    action :create
  end if File.file?(curr_path)
  directory "/some/target/dir/#{File.dirname(curr_path)}" do
    path curr_path
    owner 'root'
    group 'root'
    mode 0755
    action :create
  end if File.directory?(curr_path)
end

Esto es solo una idea, porque las sub-rutas en este ejemplo no se manejan correctamente.

 99
Author: CAMOBAP,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-01 08:08:53

Lo conseguí trabajando usando bash recurso como abajo:

bash "install_jettyhightide" do
  code <<-EOL
  unzip /var/tmp/jetty-hightide-7.4.5.v20110725.zip -d /opt/jetty/
  mv /opt/jetty/jetty-hightide-7.4.5.v20110725/* /opt/jetty/
  cp /opt/jetty/bin/jetty.sh /etc/init.d/jetty
  update-rc.d jetty defaults
  EOL
end

Pero realmente esperaba una forma chef de hacerlo. copiar / mover archivos localmente sería la tarea más genérica que un administrador de sistema necesitará hacer.

 13
Author: slayedbylucifer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-07 10:38:24

Sé que esta pregunta ya ha sido respondida y discutida, pero aquí está el método que uso al crear archivos.

  1. Primero incluya el archivo debajo de los archivos/carpeta predeterminada del libro de cocina
  2. Luego en su receta use el recurso cookbook_file

Ej:

cookbook_file "/server/path/to/file.ext" do
  source "filename.ext"
  owner "root"
  group "root"
  mode 00600
  action :create_if_missing
end

De la documentación del chef: http://docs.opscode.com/resource_cookbook_file.html

El recurso cookbook_file se utiliza para transferir archivos desde sub-directorio de la archivos / directorio en un libro de cocina a un especificado ruta que se encuentra en el host ejecutando el chef-cliente o chef-solo.

 13
Author: Marcos Abreu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-17 21:45:37

Podrías probar el libro de cocina ark. Esto extrae el archivo para usted y luego nota un recurso execute.

 9
Author: StephenKing,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-07 17:32:03

Además de la forma en que lo has hecho y aceptado, si solo quisieras ejecutar un comando como lo pediste inicialmente (copiar o mover), y no ejecutar un bloque de comandos, entonces podrías hacerlo con el recurso execute:

execute "copy_core" do
    command "mv /var/tmp/jetty-hightide-7.4.5.v20110725 /opt/jetty"
    user "root"
end

Tal vez esto ayude a alguien más a mirar esto en el futuro.

 2
Author: Titi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-23 10:42:42

En realidad usaría algo como lo siguiente (observe "binread") ya que esto funcionaría para archivos de texto y archivos binarios. el uso de "read" produciría resultados sorprendentes con archivos binarios, particularmente si usa sistemas unix y windows.

file destination do
  content IO.binread(source)
  action  :create
end
 1
Author: rui,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-20 06:50:04

Para copiar archivos localmente en CHEF

file "C:/Users/Administrator/chef/1.xml" 

do         --->       tar content lazy 

{

IO.read("C:/Users/Administrator/chef-repo/cookbooks/2.xml")

} -->src

action :create

end
 0
Author: Jasper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-21 10:36:24

Si su receta ya está vinculada a Windows, puede usar scripts de PowerShell incrustados, como estos:

# Copy files from "C:/foo/lib" to "C:/foo"
powershell_script "copy_lib" do
  code <<-EOH
    $ErrorActionPreference = "Stop"
      Get-ChildItem -Path "C:/foo/lib" -File | Foreach-Object {
        Copy-Item -Path $_.Fullname -Destination "C:/foo" -Force
      }
  EOH
end

# Delete "C:/foo/lib" folder
powershell_script "delete_lib" do
  code <<-EOH
    $ErrorActionPreference = "Stop"
    Remove-Item -Path "C:/foo/lib" -Recurse
  EOH
end
 0
Author: JoelFan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-08 18:56:30