Class: Scalarium::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/scalarium/cli.rb

Defined Under Namespace

Classes: AppNotFound, CloudNotFound, RolOrInstanceNotFound

Instance Method Summary collapse

Instance Method Details

#apps(app_name = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/scalarium/cli.rb', line 161

def apps(app_name = nil)
  with_scalarium do |scalarium|
    if app_name
      app = scalarium.find_app(app_name) or raise AppNotFound.new(app_name)
      cool_inspect(app)
    else
      scalarium.apps.each { |app| say app.name, Color::BLUE }
    end
  end
end

#clouds(cloud_name = "all") ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/scalarium/cli.rb', line 175

def clouds(cloud_name = "all")
  with_scalarium(cloud_name) do |scalarium,clouds|

    clouds.threaded_each{ |c|
      if cloud_name != 'all'
        DQ[ lambda{ c.instances }, lambda{c.roles} ]
        Thread.exclusive do
          print_cloud(c, options[:verbose])
        end
      else
        puts c.name
      end
    }

  end
end

#configure(rol_or_instance) ⇒ Object



114
115
116
117
# File 'lib/scalarium/cli.rb', line 114

def configure(rol_or_instance)
  # /clouds/07/instances/e5/chef?chef_action=setup
  # /clouds/07/instances/e5/chef?chef_action=configure
end

#deploy(name) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/scalarium/cli.rb', line 143

def deploy(name)
  with_scalarium do |scalarium|
    app = scalarium.find_app(name) or raise AppNotFound.new(name)

    deploy_info = app.deploy!

    puts "Waiting the deploy finish"
    puts "Check https://manage.scalarium.com/applications/#{app.id}/deployments/#{deploy_info["id"]} if you want"
    while deploy_info["status"] == 'running'
      sleep 1
      deploy_info = app.deploy_info(deploy_info["id"])
    end
    puts "Deploy was #{deploy_info["status"]}"
    exit (deploy_info["status"] == 'successful' ? 0 : -1)
  end
end

#execute(cloud_name, rol_or_instance, command) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/scalarium/cli.rb', line 53

def execute(cloud_name, rol_or_instance, command)
  with_scalarium(cloud_name) do |scalarium, clouds|

    clouds.each do |cloud|
      instances = cloud.find_instances(rol_or_instance) or raise RolOrInstanceNotFound.new(rol_or_instance)

      instances.threaded_each do |instance|
        run_remote_command(instance, command)
      end
    end
  end
end

#run_recipe(cloudname, rol_or_instances, recipes_names) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/scalarium/cli.rb', line 120

def run_recipe(cloudname, rol_or_instances, recipes_names)
  with_scalarium(cloudname) do |scalarium, clouds|

    clouds.each do |cloud|

      instances = cloud.find_instances(rol_or_instances) or raise RolOrInstanceNotFound.new(rol_or_instances)
      instance_ids = instances.map{|i| i.id}

      deploy_info = cloud.run_recipe!(recipes_names, instance_ids)

      puts "Waiting the recipe to finish"
      puts "Check https://manage.scalarium.com/clouds/#{cloud.id}/deployments/#{deploy_info["id"]} if you want"
      while deploy_info["status"] == 'running'
        sleep 1
        deploy_info = cloud.check_deploy(deploy_info["id"])
      end
      puts "Deploy was #{deploy_info["status"]}"
      exit (deploy_info["status"] == 'successful' ? 0 : -1)
    end
  end
end

#run_remote(cloud_name, rol_or_instance, command) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/scalarium/cli.rb', line 67

def run_remote(cloud_name, rol_or_instance, command)
  with_scalarium(cloud_name) do |scalarium, clouds|

    instances = []
    clouds.each do |cloud|
      instances = cloud.find_instances(rol_or_instance) or raise RolOrInstanceNotFound.new(rol_or_instance)
    end
    instances.flatten!

    total_instances = instances.size
    command = "#{command} ; read"
    session_name = "scalarium_gem#{Time.now.to_i}#{Time.now.usec}"
    puts "Launching in #{instances.map{|i| i.nickname}}"
    system(%{tmux new-session -d -n #{session_name} -s #{session_name} "ssh -t #{instances.shift.nickname} \\"#{command}\\""})
    while(instance = instances.shift) do
      system(%{tmux split-window -h -p #{100/total_instances*(instances.size+1)} -t #{session_name} "ssh -t #{instance.nickname} \\"#{command}\\""})
    end
    system("tmux select-window -t #{session_name} ")
    system("tmux select-layout -t #{session_name} tiled")
    exec("tmux -2 attach-session -t #{session_name} ")

  end
end

#sshconfig(cloud_names) ⇒ Object



16
17
18
19
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
# File 'lib/scalarium/cli.rb', line 16

def sshconfig(cloud_names)
  with_scalarium(cloud_names) do |scalarium,clouds|


    config_file = File.expand_path("~/.ssh/config")
    header      = "# Added by scalarium"
    tail        = "# Do not modify"

    if File.exists?(config_file)
      config = File.read(config_file)
      config.gsub!(/#{header}.*#{tail}/mi,'')
    else
      config = ""
    end

    config  << header << "\n"

    clouds.threaded_each do |cloud|
      cloud.instances.each do |instance|
        Thread.exclusive do
          say_status(cloud.name, instance.nickname)
          config << format_ssh_config_host(instance)
        end
      end
    end

    config << "\n" << tail << "\n"

    File.open(config_file,'w'){|f|
      f.rewind
      f.write config
    }
    say("Configuration file updated")
  end
end

#update_cookbooks(cloud_name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/scalarium/cli.rb', line 93

def update_cookbooks(cloud_name)
  with_scalarium(cloud_name) do |scalarium,clouds|

    clouds.each do |cloud|
      puts "Updating cookbooks for #{cloud.name}"

      deploy_info = cloud.update_cookbooks!

      puts "Waiting the deploy finish"
      puts "Check https://manage.scalarium.com/clouds/#{cloud.id}/deployments/#{deploy_info["id"]} if you want"
      while deploy_info["status"] == 'running'
        sleep 1
        deploy_info = cloud.check_deploy(deploy_info["id"])
      end
      puts "Deploy was #{deploy_info["status"]}"
      exit (deploy_info["status"] == 'successful' ? 0 : -1)
    end
  end
end