Class: Commands::BuildDeployConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/build_deploy_config.rb

Overview

this class builds up the contents of the json deploy file that we feed to the remote system to kickstart the chef run. It allows us to dynamically configure the options we want

Class Method Summary collapse

Class Method Details

.build_json(zz_options, deploy_file) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/commands/build_deploy_config.rb', line 8

def self.build_json(zz_options, deploy_file)
  chef_config = { :run_list => "recipe[deploy-manager]" }
  zz_options[:dev_machine] = false
  chef_config[:zz] = zz_options

  json = JSON.pretty_generate(chef_config)

  # now build up the command string to run
  cmd =
"bash -l -c '(
cat <<'EOP'
#{json}
EOP
) > /var/chef/#{deploy_file}.json
cd #{::ZZDeploy::RECIPES_DIR}
sudo bundle exec chef-solo -l debug -c chef-local/remote_solo.rb -j /var/chef/#{deploy_file}.json'"

  return cmd
end

.do_app_deploy(utils, amazon, instances, group_name, deploy_group, migrate_command, downtime, no_restart, result_path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/commands/build_deploy_config.rb', line 95

def self.do_app_deploy(utils, amazon, instances, group_name, deploy_group, migrate_command, downtime, no_restart, result_path)
  # ok, phase one is to do everything up to but not including the restart
  begin
    utils.mark_deploy_state(instances, :deploy_app, ZZSharedLib::Utils::START)
    remote_cmd = remote_app_deploy(migrate_command, downtime)
    multi = MultiSSH.new(amazon, group_name, deploy_group)
    multi.run_instances(instances, remote_cmd)
  rescue Exception => ex
    raise ex
  ensure
    multi.output_tracked_data_to_files("app_deploy", result_path) rescue nil
  end

  # the prep is good and all servers are ready to restart
  begin
    if no_restart == false
      puts "Restarting servers..."
      utils.mark_deploy_state(instances, :deploy_app, ZZSharedLib::Utils::RESTARTING)
      remote_cmd = remote_app_restart(migrate_command, downtime)
      multi.run_instances(instances, remote_cmd)
    end
  rescue Exception => ex
    raise ex
  ensure
    multi.output_tracked_data_to_files("app_restart", result_path) rescue nil
    # mark as ready unless they are in the error state already
    utils.mark_deploy_state(instances, :deploy_app, ZZSharedLib::Utils::READY, true)
  end
end

.do_config_deploy(utils, amazon, instances, group_name, deploy_group, result_path) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/commands/build_deploy_config.rb', line 80

def self.do_config_deploy(utils, amazon, instances, group_name, deploy_group, result_path)
  begin
    utils.mark_deploy_state(instances, :deploy_chef, ZZSharedLib::Utils::START)
    remote_cmd = remote_config_deploy
    multi = MultiSSH.new(amazon, group_name, deploy_group)
    multi.run_instances(instances, remote_cmd)
  rescue Exception => ex
    raise ex
  ensure
    multi.output_tracked_data_to_files("deploy_chef", result_path) rescue nil
    # only mark ready if not in error state
    utils.mark_deploy_state(instances, :deploy_chef, ZZSharedLib::Utils::READY, true)
  end
end

.do_maint_deploy(utils, amazon, instances, group_name, deploy_group, maint, result_path) ⇒ Object

turn off or on the maintenance mode



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/commands/build_deploy_config.rb', line 126

def self.do_maint_deploy(utils, amazon, instances, group_name, deploy_group, maint, result_path)
  begin
    utils.mark_deploy_state(instances, :deploy_app, ZZSharedLib::Utils::MAINT)
    remote_cmd = remote_app_maint(maint)
    multi = MultiSSH.new(amazon, group_name, deploy_group)
    multi.run_instances(instances, remote_cmd)
  rescue Exception => ex
    raise ex
  ensure
    multi.output_tracked_data_to_files("app_maint", result_path) rescue nil
    # only mark ready if not in error state
    utils.mark_deploy_state(instances, :deploy_app, ZZSharedLib::Utils::READY, true)
  end
end

.do_remote_shutdown(utils, amazon, instances, group_name, deploy_group, result_path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/commands/build_deploy_config.rb', line 68

def self.do_remote_shutdown(utils, amazon, instances, group_name, deploy_group, result_path)
  begin
    remote_cmd = remote_shutdown
    multi = MultiSSH.new(amazon, group_name, deploy_group)
    multi.run_instances(instances, remote_cmd)
  rescue Exception => ex
    # ignore any errors since we want to shut down regardless
  ensure
    multi.output_tracked_data_to_files("remote_shutdown", result_path) rescue nil
  end
end

.remote_app_deploy(migrate_command, downtime) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/commands/build_deploy_config.rb', line 28

def self.remote_app_deploy(migrate_command, downtime)
  zz = {
      :deploy_what => "app",
      :deploy_migrate_command => migrate_command,
      :deploy_downtime => downtime
  }
  return build_json(zz, 'deploy_app')
end

.remote_app_maint(maint) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/commands/build_deploy_config.rb', line 46

def self.remote_app_maint(maint)
  zz = {
      :deploy_what => "app_maint",
      :deploy_maint => maint
  }
  return build_json(zz, 'deploy_app_maint')
end

.remote_app_restart(migrate_command, downtime) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/commands/build_deploy_config.rb', line 37

def self.remote_app_restart(migrate_command, downtime)
  zz = {
      :deploy_what => "app_restart",
      :deploy_migrate_command => migrate_command,
      :deploy_downtime => downtime
  }
  return build_json(zz, 'deploy_app_restart')
end

.remote_config_deployObject



54
55
56
57
58
59
# File 'lib/commands/build_deploy_config.rb', line 54

def self.remote_config_deploy
  zz = {
      :deploy_what => "config"
  }
  return build_json(zz, 'deploy_config')
end

.remote_shutdownObject



61
62
63
64
65
66
# File 'lib/commands/build_deploy_config.rb', line 61

def self.remote_shutdown
  zz = {
      :deploy_what => "shutdown"
  }
  return build_json(zz, 'deploy_shutdown')
end