ssvm_cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ chefkitchen_cli
2
+ ===============
3
+
4
+ CLI for talking to chefkitchen to do quick chef tasks
data/bin/ssvm ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
+
4
+ require 'fileutils'
5
+ require 'yaml'
6
+ require 'rubygems'
7
+ require 'thor'
8
+ require 'cookie_jar'
9
+ require 'request_helper'
10
+
11
+ class Ssvm < Thor
12
+ CONFIG_FILE = File.expand_path('~/.ssvm.yml')
13
+
14
+ desc 'create_vm', 'submit a VM creation request'
15
+ method_option :json, :desc => "Return result in json format"
16
+ method_option :desired_hostname, :aliases => '-n', :required => true, :desc => "Name for the VM"
17
+ method_option :owners, :aliases => '-o', :desc => "Owners of the VM", :type => :array
18
+ method_option :env, :aliases => '-e', :required => true, :desc => "Environment for the VM (e.g. prod or np)"
19
+ method_option :datacenter, :aliases => '-d', :required => true, :desc => "Datacenter for the VM (e.g. dc1 or dc2)"
20
+ method_option :build_spec, :required => true, :desc => "Default or customized VM request"
21
+ method_option :os, :desc => "What OS to use for the VM (e.g. Scientific Linux (Carbon) 6.0 x86_64)"
22
+ method_option :instances, :desc => "How many instances to make"
23
+ method_option :start_at, :desc => "What number to start at for multi instances request", :default => 1
24
+ method_option :optimal_spread, :desc => "Optimal spread for multi instances request", :default => "Rack"
25
+ method_option :minimal_spread, :desc => "Minimal spread for multi instances request", :default => "Rack"
26
+ method_option :memory, :aliases => '-m', :desc => "How much memory for the VM"
27
+ method_option :cpu, :aliases => '-c', :desc => "How CPU for the VM"
28
+ method_option :disk, :desc => "How much disk space for the VM"
29
+ method_option :no_exp, :type => :boolean, :desc => "No expiration for the VM"
30
+ method_option :justifications, :aliases => '-j', :desc => "Justification for any special request"
31
+ def create_vm
32
+ conf = load_conf(options[:conf_file] || CONFIG_FILE)
33
+ rhelper = RequestHelper.new(conf)
34
+ rhelper.create_vm(options)
35
+ end
36
+
37
+ desc "config", "configuration setup"
38
+ method_option :conf_file, :aliases => "-c", :desc => "Where to write config to"
39
+ def config
40
+ config_file = File.expand_path(options[:conf_file] || CONFIG_FILE)
41
+ puts "Provide the following inputs so ssvm_cli can save them to #{config_file} for future execution"
42
+ conf = {}
43
+ conf[:username] = ask("Username: ")
44
+ conf[:password] = ask("Password: ", true)
45
+ conf[:server] = ask("Chefkitchen server: ")
46
+ File.open(config_file, 'w') do |f|
47
+ YAML.dump(conf, f)
48
+ end
49
+ FileUtils.chmod 0700, config_file
50
+ puts "#{config_file} has been created"
51
+ end
52
+
53
+ private
54
+ def ask(str,mask=false)
55
+ begin
56
+ print str
57
+ system 'stty -echo;' if mask
58
+ input = STDIN.gets.chomp
59
+ ensure
60
+ system 'stty echo; echo ""'
61
+ end
62
+ return input
63
+ end
64
+
65
+ def load_conf(conf_file)
66
+ YAML::load(File.open(File.expand_path(conf_file)))
67
+ end
68
+ end
69
+
70
+ Ssvm.start
data/lib/cookie_jar.rb ADDED
@@ -0,0 +1,16 @@
1
+ module CookieJar
2
+ def get_cookies
3
+ params = {:login => @username, :password => @password}
4
+ cookies = nil
5
+ RestClient.post("https://#{@server}/login/login", params) do |response, request, result, &block|
6
+ if response.code == 200
7
+ cookies = response.cookies
8
+ elsif response.code == 302 && response.headers[:location] !~ /login/
9
+ cookies = response.cookies
10
+ else
11
+ raise "Failed to authenticate"
12
+ end
13
+ end
14
+ cookies
15
+ end
16
+ end
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'rest-client'
3
+ require 'json'
4
+ require 'cookie_jar'
5
+
6
+ class RequestHelper
7
+ include CookieJar
8
+
9
+ def initialize(conf)
10
+ @server = conf[:server] || "localhost"
11
+ @username = conf[:username]
12
+ @password = conf[:password]
13
+ @cookies = get_cookies
14
+ end
15
+
16
+ def create_vm(options)
17
+ options = options.dup
18
+ update_owner_field(options)
19
+ handle_multi_instances(options)
20
+ handle_custom_request(options)
21
+
22
+ begin
23
+ response = RestClient.post("http://#{@server}/vm",
24
+ {:vm_request => options},
25
+ {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
26
+ result = JSON.parse(response)
27
+ if options[:json]
28
+ puts JSON.pretty_generate(result)
29
+ else
30
+ puts result['message']
31
+ end
32
+ rescue => e
33
+ puts e.inspect
34
+ end
35
+ end
36
+
37
+ private
38
+ def update_owner_field(options)
39
+ if options['owners']
40
+ options['primary_user'] = options['owners'].shift
41
+ options['secondary_user'] = options['owners']
42
+ options.delete('owners')
43
+ else
44
+ options['primary_user'] = @username
45
+ end
46
+ end
47
+
48
+ def handle_multi_instances(options)
49
+ if options['instances']
50
+ options['multi_instances'] = true
51
+ options['multi_instances_spec_attributes[optimal_spread]'] = options['optimal_spread']
52
+ options['multi_instances_spec_attributes[minimal_spread]'] = options['minimal_spread']
53
+ options['multi_instances_spec_attributes[num_instances]'] = options['instances']
54
+ options['multi_instances_spec_attributes[start_at]'] = options['start_at']
55
+ end
56
+
57
+ options.delete('optimal_spread')
58
+ options.delete('minimal_spread')
59
+ options.delete('start_at')
60
+ options.delete('instances')
61
+ end
62
+
63
+ def handle_custom_request(options)
64
+ options['processor_count'] = options.delete('cpu')
65
+ end
66
+ end
data/ssvm_cli.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ssvm_cli"
7
+ s.version = "0.0.1"
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Darren Dao"]
10
+ s.email = ["darrendao@gmail.com"]
11
+ s.homepage = "https://github.com/darrendao/ssvm_cli"
12
+ s.summary = %q{Ruby script to talk to ssvm web services.}
13
+ s.description = %q{Ruby script to talk to ssvm web services.}
14
+
15
+ s.add_dependency 'thor'
16
+ s.add_dependency 'json'
17
+ s.add_dependency 'rest-client'
18
+ s.add_development_dependency 'yard', '~> 0.7'
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ s.extra_rdoc_files = ["README.md"]
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssvm_cli
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Darren Dao
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-30 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thor
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: json
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rest-client
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ hash: 5
71
+ segments:
72
+ - 0
73
+ - 7
74
+ version: "0.7"
75
+ type: :development
76
+ version_requirements: *id004
77
+ description: Ruby script to talk to ssvm web services.
78
+ email:
79
+ - darrendao@gmail.com
80
+ executables:
81
+ - ssvm
82
+ extensions: []
83
+
84
+ extra_rdoc_files:
85
+ - README.md
86
+ files:
87
+ - README.md
88
+ - bin/ssvm
89
+ - lib/cookie_jar.rb
90
+ - lib/request_helper.rb
91
+ - ssvm_cli.gemspec
92
+ homepage: https://github.com/darrendao/ssvm_cli
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options: []
97
+
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.11
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Ruby script to talk to ssvm web services.
125
+ test_files: []
126
+
127
+ has_rdoc: