Class: Sunzi::Command

Inherits:
Object show all
Includes:
Actions::Delegate
Defined in:
lib/sunzi/command.rb

Instance Method Summary collapse

Methods included from Actions::Delegate

included

Instance Method Details

#compile(role = nil) ⇒ Object

[View source]

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sunzi/command.rb', line 60

def compile(role = nil)
  abort_with 'You must be in a sunzi folder' unless File.exist?('sunzi.yml')
  abort_with "#{role} doesn't exist!" if role && !File.exist?("roles/#{role}.sh")
  abort_with 'As of v2, "attributes" are now "vars" in sunzi.yml and shell scripts.' if config.attributes

  # Retrieve remote recipes via HTTP
  (config.recipes || []).each do |key, value|
    dest = "compiled/recipes/#{key}.sh"
    next if config.preferences.cache_remote_recipes && File.exist?(dest)
    get value, dest
  end

  @vars = config.vars # Used within ERB templates

  # Copy local files to compiled folder
  files = glob('{recipes,roles,files}/**/*').select { |file| File.file?(file) }

  files.each do |file|
    render file, "compiled/#{file}"
  end

  # Copy files specified in sunzi.yml
  (config.files || []).each do |file|
    render file, "compiled/files/#{File.basename(file)}"
  end

  # Build install.sh
  render 'install.sh', 'compiled/install.sh'

  # Append role at the bottom of install.sh
  if role
    append_to_file 'compiled/install.sh', "\n" + File.read("compiled/roles/#{role}.sh")
  end
end

#create(project) ⇒ Object

[View source]

10
11
12
13
14
15
16
17
18
# File 'lib/sunzi/command.rb', line 10

def create(project)
  copy_file 'templates/create/.gitignore',         "#{project}/.gitignore"
  copy_file 'templates/create/sunzi.yml',          "#{project}/sunzi.yml"
  copy_file 'templates/create/install.sh',         "#{project}/install.sh"
  copy_file 'templates/create/recipes/sunzi.sh',   "#{project}/recipes/sunzi.sh"
  copy_file 'templates/create/roles/db.sh',        "#{project}/roles/db.sh"
  copy_file 'templates/create/roles/web.sh',       "#{project}/roles/web.sh"
  copy_file 'templates/create/files/.gitkeep',     "#{project}/files/.gitkeep"
end

#deploy(target, role, options) ⇒ Object

[View source]

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sunzi/command.rb', line 20

def deploy(target, role, options)
  # compile vars and recipes
  compile(role)

  sudo = 'sudo ' if options.sudo?
  endpoint = Endpoint.new(target)

  # The host key might change when we instantiate a new VM, so
  # we remove (-R) the old host key from known_hosts.
  `ssh-keygen -R #{endpoint.host} 2> /dev/null`

  remote_commands = <<-EOS
  rm -rf ~/sunzi &&
  mkdir ~/sunzi &&
  cd ~/sunzi &&
  tar xz &&
  #{sudo}bash install.sh
  EOS

  remote_commands.strip! << ' && rm -rf ~/sunzi' if config.preferences.erase_remote_folder

  local_commands = <<-EOS
  cd compiled
  tar cz . | ssh -o 'StrictHostKeyChecking no' #{endpoint.user}@#{endpoint.host} -p #{endpoint.port} '#{remote_commands}'
  EOS

  Open3.popen3(local_commands) do |stdin, stdout, stderr|
    stdin.close
    t = Thread.new do
      while (line = stderr.gets)
        print line.color(:red)
      end
    end
    while (line = stdout.gets)
      print line.color(:green)
    end
    t.join
  end
end