Class: DevLXC::CLI::DevLXC
- Inherits:
-
Thor
- Object
- Thor
- DevLXC::CLI::DevLXC
- Defined in:
- lib/dev-lxc/cli.rb
Instance Method Summary collapse
- #attach(server_name_regex) ⇒ Object
- #bootstrap_container(base_container_name = nil, container_name) ⇒ Object
- #chef_repo ⇒ Object
- #configure_chef_client(container_name) ⇒ Object
- #create(platform_image_name = nil) ⇒ Object
- #destroy(server_name_regex = nil) ⇒ Object
- #global_status ⇒ Object
- #halt(server_name_regex = nil) ⇒ Object
- #init(topology = nil, unique_string = nil) ⇒ Object
- #install_chef_client(container_name) ⇒ Object
- #list_images(server_name_regex = nil) ⇒ Object
- #realpath(server_name_regex = nil, rootfs_path) ⇒ Object
- #run_command(server_name_regex = nil, command) ⇒ Object
- #snapshot(server_name_regex = nil) ⇒ Object
- #status(server_name_regex = nil) ⇒ Object
- #up(server_name_regex = nil) ⇒ Object
Instance Method Details
#attach(server_name_regex) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/dev-lxc/cli.rb', line 169 def attach(server_name_regex) servers = match_server_name_regex(server_name_regex) if servers.length > 1 puts "ERROR: The following servers matched '#{server_name_regex}'" servers.map { |s| puts " #{s.server.name}" } puts " Please specify a single server to attach to" exit 1 elsif servers.empty? puts "ERROR: No servers matched '#{server_name_regex}'" puts " Please specify a single server to attach to" exit 1 end server = servers.first.server unless server.defined? && server.running? puts "ERROR: Server '#{server.name}' is not running" exit 1 end attach_opts = { wait: true, env_policy: LXC::LXC_ATTACH_CLEAR_ENV, extra_env_vars: ["LANG=en_US.UTF-8", "TERM=linux", "HOME=#{ENV['HOME']}"] } shell = ENV['SHELL'] server.attach(attach_opts) { system(shell) } end |
#bootstrap_container(base_container_name = nil, container_name) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/dev-lxc/cli.rb', line 90 def bootstrap_container(base_container_name=nil, container_name) start_time = Time.now chef_server_url = [:chef_server_url] validation_client_name = [:validation_client_name] validation_key = [:validation_key] if chef_server_url.nil? && validation_client_name.nil? && validation_key.nil? cluster = get_cluster([:config]) chef_server_bootstrap_backend = ::DevLXC::Container.new(cluster.chef_server_bootstrap_backend, cluster.lxc_config_path) unless chef_server_bootstrap_backend.defined? puts "ERROR: Can not copy validation key because Chef Server '#{chef_server_bootstrap_backend.name}' is not created." exit 1 end chef_server_url = "https://#{cluster.api_fqdn}/organizations/ponyville" validation_client_name = 'ponyville-validator' validation_key = "#{chef_server_bootstrap_backend.config_item('lxc.rootfs')}/root/chef-repo/.chef/ponyville-validator.pem" elsif chef_server_url.nil? || validation_client_name.nil? || validation_key.nil? puts "ERROR: All of the --chef-server-url, --validation-client-name and --validation-key options must be set or left unset. Do not set only some of these options." exit 1 end container = ::DevLXC::Container.new(container_name) container.bootstrap_container(base_container_name, [:version], [:run_list], chef_server_url, validation_client_name, validation_key) puts print_elapsed_time(Time.now - start_time) end |
#chef_repo ⇒ Object
199 200 201 |
# File 'lib/dev-lxc/cli.rb', line 199 def chef_repo get_cluster([:config]).chef_repo([:force], [:pivotal]) end |
#configure_chef_client(container_name) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dev-lxc/cli.rb', line 58 def configure_chef_client(container_name) start_time = Time.now chef_server_url = [:chef_server_url] validation_client_name = [:validation_client_name] validation_key = [:validation_key] if chef_server_url.nil? && validation_client_name.nil? && validation_key.nil? cluster = get_cluster([:config]) chef_server_bootstrap_backend = ::DevLXC::Container.new(cluster.chef_server_bootstrap_backend, cluster.lxc_config_path) unless chef_server_bootstrap_backend.defined? puts "ERROR: Can not copy validation key because Chef Server '#{chef_server_bootstrap_backend.name}' is not created." exit 1 end chef_server_url = "https://#{cluster.api_fqdn}/organizations/ponyville" validation_client_name = 'ponyville-validator' validation_key = "#{chef_server_bootstrap_backend.config_item('lxc.rootfs')}/root/chef-repo/.chef/ponyville-validator.pem" elsif chef_server_url.nil? || validation_client_name.nil? || validation_key.nil? puts "ERROR: All of the --chef-server-url, --validation-client-name and --validation-key options must be set or left unset. Do not set only some of these options." exit 1 end container = ::DevLXC::Container.new(container_name) container.configure_chef_client(chef_server_url, validation_client_name, validation_key) puts print_elapsed_time(Time.now - start_time) end |
#create(platform_image_name = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dev-lxc/cli.rb', line 29 def create(platform_image_name=nil) start_time = Time.now platform_image_names = %w(p-ubuntu-1204 p-ubuntu-1404 p-centos-5 p-centos-6) if platform_image_name.nil? || ! platform_image_names.include?(platform_image_name) platform_image_names_with_index = platform_image_names.map.with_index{ |a, i| [i+1, *a]} print_table platform_image_names_with_index selection = ask("Which platform image do you want to create?", :limited_to => platform_image_names_with_index.map{|c| c[0].to_s}) platform_image_name = platform_image_names[selection.to_i - 1] end ::DevLXC.create_platform_image(platform_image_name) puts print_elapsed_time(Time.now - start_time) end |
#destroy(server_name_regex = nil) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/dev-lxc/cli.rb', line 297 def destroy(server_name_regex=nil) start_time = Time.now match_server_name_regex(server_name_regex).reverse_each do |s| s.destroy s.destroy_image(:custom) if [:custom] s.destroy_image(:unique) if [:unique] s.destroy_image(:shared) if [:shared] s.destroy_image(:platform) if [:platform] puts end print_elapsed_time(Time.now - start_time) end |
#global_status ⇒ Object
140 141 142 143 144 145 |
# File 'lib/dev-lxc/cli.rb', line 140 def global_status containers = Array.new LXC::list_containers({config_path: '/var/lib/dev-lxc'}).map { |c| containers << ::DevLXC::Container.new(c, '/var/lib/dev-lxc').status } max_container_name_length = containers.max_by { |c| c['name'].length }['name'].length unless containers.empty? containers.each { |c| printf "%#{max_container_name_length}s %-15s %s\n", c['name'], c['state'], c['ip_addresses'] } end |
#halt(server_name_regex = nil) ⇒ Object
258 259 260 261 262 |
# File 'lib/dev-lxc/cli.rb', line 258 def halt(server_name_regex=nil) start_time = Time.now match_server_name_regex(server_name_regex).reverse_each { |s| s.stop; puts } print_elapsed_time(Time.now - start_time) end |
#init(topology = nil, unique_string = nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/dev-lxc/cli.rb', line 116 def init(topology=nil, unique_string=nil) topologies = %w(adhoc open-source standalone tier) if topology.nil? || ! topologies.include?(topology) topologies_with_index = topologies.map.with_index{ |a, i| [i+1, *a]} print_table topologies_with_index selection = ask("Which cluster topology do you want to use?", :limited_to => topologies_with_index.map{|c| c[0].to_s}) topology = topologies[selection.to_i - 1] end config = IO.read("#{File.dirname(__FILE__)}/../../files/configs/#{topology}.yml") unless unique_string.nil? config_hash = YAML.load(config.gsub(/^#/, '')) config.gsub!(/api_fqdn:\s+#{config_hash['api_fqdn']}/, "api_fqdn: #{unique_string}#{config_hash['api_fqdn']}") config.gsub!(/analytics_fqdn:\s+#{config_hash['analytics_fqdn']}/, "analytics_fqdn: #{unique_string}#{config_hash['analytics_fqdn']}") config_hash['chef-server']['servers'].keys.each do |server_name| config.gsub!(/ #{server_name}:/, " #{unique_string}#{server_name}:") end config_hash['analytics']['servers'].keys.each do |server_name| config.gsub!(/ #{server_name}:/, " #{unique_string}#{server_name}:") end end puts config end |
#install_chef_client(container_name) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/dev-lxc/cli.rb', line 45 def install_chef_client(container_name) start_time = Time.now container = ::DevLXC::Container.new(container_name) container.install_chef_client([:version]) puts print_elapsed_time(Time.now - start_time) end |
#list_images(server_name_regex = nil) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/dev-lxc/cli.rb', line 205 def list_images(server_name_regex=nil) lxc_config_path = get_cluster([:config]).lxc_config_path images = Hash.new { |h,k| h[k] = Hash.new { |h,k| h[k] = Array.new } } match_server_name_regex(server_name_regex).each do |s| images[s.platform_image_name][s.shared_image_name] << s.server.name end images.each_with_index do |(platform_name, shared), images_index| shared.each_with_index do |(shared_name, final), shared_index| printf "Platform: %27s %s\n", (LXC::Container.new(platform_name, lxc_config_path).defined? ? "Created" : "Not Created"), platform_name unless shared_name.empty? puts "|" printf "\\_ Shared: %26s %s\n", (LXC::Container.new(shared_name, lxc_config_path).defined? ? "Created" : "Not Created"), shared_name end final.each_with_index do |final_name, final_index| puts " |" unique_name = "u-#{final_name}" printf " \\_ Unique: %23s %s\n", (LXC::Container.new(unique_name, lxc_config_path).defined? ? "Created" : "Not Created"), unique_name shared_connector = (final_index + 1 < final.length ? "|" : " ") custom_name = "c-#{final_name}" if LXC::Container.new(custom_name, lxc_config_path).defined? printf " #{shared_connector} \\_ Custom: %20s %s\n", "Created", custom_name custom_spacing = " " final_width = 11 else final_width = 14 end printf " #{shared_connector} #{custom_spacing}\\_ Final Server: %#{final_width}s %s\n", (LXC::Container.new(final_name, lxc_config_path).defined? ? "Created" : "Not Created"), final_name end puts if (shared_index + 1 < shared.length) || (images_index + 1 < images.length) end end end |
#realpath(server_name_regex = nil, rootfs_path) ⇒ Object
161 162 163 164 165 |
# File 'lib/dev-lxc/cli.rb', line 161 def realpath(server_name_regex=nil, rootfs_path) realpath = Array.new match_server_name_regex(server_name_regex).map { |s| realpath << s.realpath(rootfs_path) } puts realpath.compact end |
#run_command(server_name_regex = nil, command) ⇒ Object
242 243 244 245 246 |
# File 'lib/dev-lxc/cli.rb', line 242 def run_command(server_name_regex=nil, command) start_time = Time.now match_server_name_regex(server_name_regex).each { |s| s.run_command(command); puts } print_elapsed_time(Time.now - start_time) end |
#snapshot(server_name_regex = nil) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/dev-lxc/cli.rb', line 267 def snapshot(server_name_regex=nil) start_time = Time.now non_stopped_servers = Array.new existing_custom_images = Array.new lxc_config_path = get_cluster([:config]).lxc_config_path match_server_name_regex(server_name_regex).each do |s| non_stopped_servers << s.server.name if s.server.state != :stopped existing_custom_images << s.server.name if LXC::Container.new("c-#{s.server.name}", lxc_config_path).defined? end unless non_stopped_servers.empty? puts "ERROR: Aborting snapshot because the following servers are not stopped" puts non_stopped_servers exit 1 end unless existing_custom_images.empty? || [:force] puts "ERROR: The following servers already have a custom image" puts " Use the `--force` or `-f` option to overwrite existing custom images" puts existing_custom_images exit 1 end match_server_name_regex(server_name_regex).each { |s| s.snapshot([:force]); puts } print_elapsed_time(Time.now - start_time) end |
#status(server_name_regex = nil) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/dev-lxc/cli.rb', line 149 def status(server_name_regex=nil) cluster = get_cluster([:config]) puts "Chef Server: https://#{cluster.api_fqdn}\n\n" if cluster.api_fqdn puts "Analytics: https://#{cluster.analytics_fqdn}\n\n" if cluster.analytics_fqdn servers = Array.new match_server_name_regex(server_name_regex).map { |s| servers << s.server.status } max_server_name_length = servers.max_by { |s| s['name'].length }['name'].length unless servers.empty? servers.each { |s| printf "%#{max_server_name_length}s %-15s %s\n", s['name'], s['state'], s['ip_addresses'] } end |
#up(server_name_regex = nil) ⇒ Object
250 251 252 253 254 |
# File 'lib/dev-lxc/cli.rb', line 250 def up(server_name_regex=nil) start_time = Time.now match_server_name_regex(server_name_regex).each { |s| s.start; puts } print_elapsed_time(Time.now - start_time) end |