jdc 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ module JDC::Micro
2
+ class VMrun
3
+ attr_reader :vmx, :vmrun
4
+
5
+ def initialize(config)
6
+ @platform = config['platform']
7
+ @user = 'root' # must use root as we muck around with system settings
8
+ @password = config['password']
9
+ @vmrun = config['vmrun']
10
+ @vmx = config['vmx']
11
+
12
+ # TODO honor TMPDIR
13
+ if @platform == :windows
14
+ @temp_dir = ENV['temp']
15
+ else
16
+ @temp_dir = '/tmp'
17
+ end
18
+ end
19
+
20
+ def connection_type
21
+ read_variable('ethernet0.connectionType')
22
+ end
23
+
24
+ def connection_type=(type)
25
+ write_variable("ethernet0.connectionType", type)
26
+ end
27
+
28
+ def nat?
29
+ connection_type == "nat"
30
+ end
31
+
32
+ def bridged?
33
+ connection_type == "bridged"
34
+ end
35
+
36
+ def domain
37
+ # switch to Dir.mktmpdir
38
+ state_config = JDC::Micro.escape_path(File.join(@temp_dir, 'state.yml'))
39
+ run('CopyFileFromGuestToHost', "/var/vcap/bosh/state.yml #{state_config}")
40
+ bosh_config = YAML.load_file(state_config)
41
+ bosh_config['properties']['domain']
42
+ end
43
+
44
+ def ip
45
+ # switch to Dir.mktmpdir
46
+ path = JDC::Micro.escape_path(JDC::Micro.config_file('refresh_ip.rb'))
47
+ ip_file = JDC::Micro.escape_path(File.join(@temp_dir, 'ip.txt'))
48
+ run('CopyFileFromHostToGuest', "#{path} /tmp/refresh_ip.rb")
49
+ run('runProgramInGuest', '/tmp/refresh_ip.rb')
50
+ run('CopyFileFromGuestToHost', "/tmp/ip.txt #{ip_file}")
51
+ File.open(ip_file, 'r') { |file| file.read }
52
+ end
53
+
54
+ def list
55
+ vms = run("list")
56
+ vms.delete_if { |line| line =~ /^Total/ }
57
+ vms.map { |line| JDC::Micro.escape_path(File.expand_path(line)) }
58
+ end
59
+
60
+ def offline?
61
+ command = "-gu #{@user} -gp #{@password} runProgramInGuest"
62
+ args = '/usr/bin/test -e /var/vcap/micro/offline'
63
+ # why not use run_command?
64
+ result = %x{#{@vmrun} #{command} #{@vmx} #{args}}
65
+
66
+ if result.include?('Guest program exited with non-zero exit code: 1')
67
+ return false
68
+ elsif $?.exitstatus == 0
69
+ return true
70
+ else
71
+ raise "failed to execute vmrun:\n#{result}"
72
+ end
73
+ end
74
+
75
+ def offline!
76
+ path = JDC::Micro.escape_path(JDC::Micro.config_file('offline.conf'))
77
+ run('CopyFileFromHostToGuest', "#{path} /etc/dnsmasq.d/offline.conf")
78
+ run('runProgramInGuest', '/usr/bin/touch /var/vcap/micro/offline')
79
+ run('runProgramInGuest',
80
+ "/bin/sed -i -e 's/^[^#]/# &/g' /etc/dnsmasq.d/server || true")
81
+ restart_dnsmasq
82
+ end
83
+
84
+ def online!
85
+ run('runProgramInGuest', '/bin/rm -f /etc/dnsmasq.d/offline.conf')
86
+ run('runProgramInGuest', '/bin/rm -f /var/vcap/micro/offline')
87
+ run('runProgramInGuest',
88
+ "/bin/sed -i -e 's/^# //g' /etc/dnsmasq.d/server || true")
89
+ restart_dnsmasq
90
+ end
91
+
92
+ # check to see if the micro cloud has been configured
93
+ # uses default password to check
94
+ def ready?
95
+ command = "-gu root -gp 'ca$hc0w' runProgramInGuest"
96
+ args = '/usr/bin/test -e /var/vcap/micro/micro.json'
97
+ result = %x{#{@vmrun} #{command} #{@vmx} #{args}}
98
+
99
+ if result.include?('Invalid user name or password for the guest OS') || $?.exitstatus == 0
100
+ return true
101
+ elsif $?.exitstatus == 1
102
+ return false
103
+ else
104
+ raise "failed to execute vmrun:\n#{result}"
105
+ end
106
+ end
107
+
108
+ def read_variable(var)
109
+ # TODO deal with non-ok return
110
+ run("readVariable", "runtimeConfig #{var}").first
111
+ end
112
+
113
+ def write_variable(var, value)
114
+ run('writeVariable', "runtimeConfig #{var} #{value}")
115
+ end
116
+
117
+ def reset
118
+ run('reset', 'soft')
119
+ end
120
+
121
+ def restart_dnsmasq
122
+ # restart command doesn't always work, start and stop seems to be more reliable
123
+ run('runProgramInGuest', '/etc/init.d/dnsmasq stop')
124
+ run('runProgramInGuest', '/etc/init.d/dnsmasq start')
125
+ end
126
+
127
+ def run(command, args=nil)
128
+ if command.include?('Guest')
129
+ command = "-gu #{@user} -gp #{@password} #{command}"
130
+ end
131
+ JDC::Micro.run_command(@vmrun, "#{command} #{@vmx} #{args}")
132
+ end
133
+
134
+ def running?
135
+ vms = list
136
+ if @platform == :windows
137
+ vms.map! { |x| x.downcase }
138
+ vms.include?(@vmx.downcase)
139
+ else
140
+ # Handle vmx being in a symlinked dir.
141
+ real_path = nil
142
+ begin
143
+ real_path = File.realpath(@vmx)
144
+ rescue
145
+ end
146
+ vms.include?(@vmx) || (real_path && vms.include?(real_path))
147
+ end
148
+ end
149
+
150
+ def start
151
+ run('start') unless running?
152
+ end
153
+
154
+ def stop
155
+ run('stop') if running?
156
+ end
157
+
158
+ def self.locate(platform)
159
+ paths = YAML.load_file(JDC::Micro.config_file('paths.yml'))
160
+ vmrun_paths = paths[platform.to_s]['vmrun']
161
+ vmrun_exe = @platform == :windows ? 'vmrun.exe' : 'vmrun'
162
+ vmrun = JDC::Micro.locate_file(vmrun_exe, "VMware", vmrun_paths)
163
+ err "Unable to locate vmrun, please supply --vmrun option" unless vmrun
164
+ vmrun
165
+ end
166
+ end
167
+
168
+ end
data/lib/jdc/micro.rb ADDED
@@ -0,0 +1,56 @@
1
+ require 'find'
2
+
3
+ module JDC::Micro
4
+ def config_file(file)
5
+ File.join(File.dirname(__FILE__), '..', '..', 'config', 'micro', file)
6
+ end
7
+
8
+ def escape_path(path)
9
+ path = File.expand_path(path)
10
+ if RUBY_PLATFORM =~ /mingw|mswin32|cygwin/
11
+ if path.include?(' ')
12
+ return '"' + path + '"'
13
+ else
14
+ return path
15
+ end
16
+ else
17
+ return path.gsub(' ', '\ ')
18
+ end
19
+ end
20
+
21
+ def locate_file(file, directory, search_paths)
22
+ search_paths.each do |path|
23
+ expanded_path = File.expand_path(path)
24
+ if File.exists?(expanded_path)
25
+ Find.find(expanded_path) do |current|
26
+ if File.directory?(current) && current.include?(directory)
27
+ full_path = File.join(current, file)
28
+ return self.escape_path(full_path) if File.exists?(full_path)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ false
35
+ end
36
+
37
+ def run_command(command, args=nil)
38
+ # TODO switch to using posix-spawn instead
39
+ result = %x{#{command} #{args} 2>&1}
40
+ unless $?.exitstatus == 0
41
+ if block_given?
42
+ yield
43
+ else
44
+ raise "failed to execute #{command} #{args}:\n#{result}"
45
+ end
46
+ else
47
+ result.split(/\n/)
48
+ end
49
+ end
50
+
51
+ module_function :config_file
52
+ module_function :escape_path
53
+ module_function :locate_file
54
+ module_function :run_command
55
+
56
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/ruby
2
+ #encoding=utf-8
3
+
4
+ require File.expand_path('../../signer.rb', __FILE__)
5
+
6
+ module JDC
7
+ module Signature
8
+ module Version
9
+
10
+ include JDC::Signer
11
+
12
+ def generate_signature secret_key, req
13
+ return sign(secret_key, string_to_sign(req))
14
+ end
15
+
16
+ def string_to_sign req
17
+ str = [
18
+ req[:method],
19
+ req[:headers]['Content-MD5'],
20
+ req[:headers]['Content-Type'],
21
+ req[:headers]['Date'],
22
+ req[:headers]['Path']
23
+ ].join("\n")
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/jdc/signer.rb ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/ruby
2
+ #encoding=utf-8
3
+
4
+ require 'base64'
5
+ require 'openssl'
6
+
7
+ module JDC
8
+ module Signer
9
+ def sign secret_key, string_to_sign, digest = 'sha1'
10
+ return Base64.encode64(OpenSSL::HMAC.digest(digest, secret_key, string_to_sign)).strip
11
+ end
12
+ end
13
+ end
data/lib/jdc/timer.rb ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby
2
+ #encoding=utf-8
3
+
4
+ require 'time'
5
+
6
+ module JDC
7
+ module Timer
8
+ def get_time
9
+ return Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
10
+ end
11
+ end
12
+ end
data/lib/jdc.rb ADDED
@@ -0,0 +1,3 @@
1
+ module JDC; end
2
+
3
+ require 'jdc/client'
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jdc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jing Dong Cloud
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thin
16
+ requirement: &7445720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *7445720
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &7444020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *7444020
36
+ - !ruby/object:Gem::Dependency
37
+ name: em-websocket
38
+ requirement: &7442820 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *7442820
47
+ - !ruby/object:Gem::Dependency
48
+ name: async_sinatra
49
+ requirement: &7441400 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *7441400
58
+ - !ruby/object:Gem::Dependency
59
+ name: uuidtools
60
+ requirement: &7440360 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *7440360
69
+ - !ruby/object:Gem::Dependency
70
+ name: interact
71
+ requirement: &7439760 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *7439760
80
+ - !ruby/object:Gem::Dependency
81
+ name: caldecott
82
+ requirement: &7438440 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - =
86
+ - !ruby/object:Gem::Version
87
+ version: 0.0.3
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *7438440
91
+ - !ruby/object:Gem::Dependency
92
+ name: rack
93
+ requirement: &7436880 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: 1.2.0
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: *7436880
102
+ description: JDC Client for JingDong Cloud
103
+ email: jdc@jd.com
104
+ executables:
105
+ - jdc
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - README.md
110
+ - LICENSE
111
+ - caldecott_helper/server.rb
112
+ - bin/jdc
113
+ - lib/jdc/const.rb
114
+ - lib/jdc/micro/switcher/darwin.rb
115
+ - lib/jdc/micro/switcher/windows.rb
116
+ - lib/jdc/micro/switcher/base.rb
117
+ - lib/jdc/micro/switcher/linux.rb
118
+ - lib/jdc/micro/switcher/dummy.rb
119
+ - lib/jdc/micro/vmrun.rb
120
+ - lib/jdc/client.rb
121
+ - lib/jdc/signer.rb
122
+ - lib/jdc/signature/version.rb
123
+ - lib/jdc/micro.rb
124
+ - lib/jdc/timer.rb
125
+ - lib/jdc.rb
126
+ - lib/cli/manifest_helper.rb
127
+ - lib/cli/services_helper.rb
128
+ - lib/cli/tunnel_helper.rb
129
+ - lib/cli/version.rb
130
+ - lib/cli/usage.rb
131
+ - lib/cli/errors.rb
132
+ - lib/cli/console_helper.rb
133
+ - lib/cli/frameworks.rb
134
+ - lib/cli/zip_util.rb
135
+ - lib/cli/core_ext.rb
136
+ - lib/cli/commands/misc.rb
137
+ - lib/cli/commands/services.rb
138
+ - lib/cli/commands/admin.rb
139
+ - lib/cli/commands/base.rb
140
+ - lib/cli/commands/apps.rb
141
+ - lib/cli/commands/micro.rb
142
+ - lib/cli/commands/manifest.rb
143
+ - lib/cli/commands/user.rb
144
+ - lib/cli/runner.rb
145
+ - lib/cli/config.rb
146
+ - lib/cli.rb
147
+ - config/micro/paths.yml
148
+ - config/clients.yml
149
+ - config/micro/refresh_ip.rb
150
+ homepage: https://github.com/jingdong-app-engine/jdc
151
+ licenses:
152
+ - Apache 2.0
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ none: false
159
+ requirements:
160
+ - - ! '>='
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ none: false
165
+ requirements:
166
+ - - ! '>='
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 1.8.11
172
+ signing_key:
173
+ specification_version: 3
174
+ summary: JDC Client for JingDong Cloud
175
+ test_files: []