knife-smucli 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61d35a867b261375cff6c92b46b7a14c8f1190e1
4
+ data.tar.gz: 5e931e8e049fbc457998b72d175ff7aaff03b865
5
+ SHA512:
6
+ metadata.gz: c3b34e2caa04a68656273af79db1e675fb22bebf8eb9dc4db91b316fa9955880cbf83ab86bb40b4fe9421d454b653c7b970bd30ed16fa718e6efd8df4ac3ece6
7
+ data.tar.gz: b28fb743edde2204dc29c5735312133090ef4addf5ccc180f6293bb0c538c285a99f877bf77e91460579115c0fcb142e508e9b713fee04f259b6f8d884ff74e2
@@ -0,0 +1,177 @@
1
+ #
2
+ # Author:: Michael Huisman (<michhuis@gmail.com>)
3
+ # Contributor::
4
+ # License:: Apache License, Version 2.0
5
+ # Version::0.0.1
6
+
7
+ require 'chef/knife'
8
+
9
+ # Base class for Smu knife commands
10
+ class Chef
11
+ class Knife
12
+ class BaseSmuCommand < Knife
13
+
14
+ deps do
15
+ require 'chef/knife/bootstrap'
16
+ Chef::Knife::Bootstrap.load_deps
17
+ require 'fog'
18
+ require 'socket'
19
+ require 'net/ssh'
20
+ require 'readline'
21
+ require 'chef/json_compat'
22
+ end
23
+
24
+ def self.get_common_options
25
+ unless defined? $default
26
+ $default = Hash.new
27
+ end
28
+
29
+ option :smu_user,
30
+ :short => "-u USERNAME",
31
+ :long => "--smuuser USERNAME",
32
+ :description => "The username for SMU"
33
+ $default[:ovmmgr_user] = "ohisc"
34
+
35
+ option :smu_pass,
36
+ :short => "-p PASSWORD",
37
+ :long => "--smupass PASSWORD",
38
+ :description => "The password for SMU"
39
+
40
+ option :smu_host,
41
+ :long => "--smuhost HOST",
42
+ :description => "The SMU host"
43
+
44
+ option :smu_port,
45
+ :long => "--smuport PORT",
46
+ :description => "The SMU CLI port number to use"
47
+ $default[:smu_port] = 8002
48
+ end
49
+
50
+ def get_config(key)
51
+ key = key.to_sym
52
+ rval = config[key] || Chef::Config[:knife][key] || $default[key]
53
+ Chef::Log.debug("value for config item #{key}: #{rval}")
54
+ rval
55
+ end
56
+
57
+ def get_cli_connection
58
+
59
+ conn_opts = {
60
+ :host => get_config(:smu_host),
61
+ :path => get_config(:smu_path),
62
+ :port => get_config(:smu_port),
63
+ :user => get_config(:smu_user),
64
+ :password => get_config(:smu_pass),
65
+ }
66
+
67
+ # Grab the smu host from the command line
68
+ # if tt is not in the config file
69
+ if not conn_opts[:host]
70
+ conn_opts[:host] = get_host
71
+ end
72
+ # Grab the password from the command line
73
+ # if tt is not in the config file
74
+ if not conn_opts[:password]
75
+ conn_opts[:password] = get_password
76
+ end
77
+ if conn_opts[:port]
78
+ Chef::Log.debug("Waiting for port #{conn_opts[:port]} on #{conn_opts[:host]}...")
79
+ tcp_test_port(conn_opts[:host],conn_opts[:port])
80
+ end
81
+ return conn_opts
82
+ end
83
+
84
+ def get_host
85
+ @host ||= ui.ask("Enter your SMU Host: ") { |q| q.echo = true }
86
+ end
87
+
88
+ def get_password
89
+ @password ||= ui.ask("Enter your password: ") { |q| q.echo = false }
90
+ end
91
+
92
+ def get_vm(vmname)
93
+ return retval
94
+ end
95
+
96
+
97
+ def fatal_exit(msg)
98
+ ui.fatal(msg)
99
+ exit 1
100
+ end
101
+
102
+ def tcp_test_port(hostname,port)
103
+ tcp_socket = TCPSocket.new(hostname, port)
104
+ readable = IO.select([tcp_socket], nil, nil, 5)
105
+ if readable
106
+ Chef::Log.debug("accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
107
+ true
108
+ else
109
+ false
110
+ end
111
+ rescue Errno::ETIMEDOUT
112
+ false
113
+ rescue Errno::EPERM
114
+ false
115
+ rescue Errno::ECONNREFUSED
116
+ sleep 2
117
+ false
118
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH
119
+ sleep 2
120
+ false
121
+ ensure
122
+ tcp_socket && tcp_socket.close
123
+ end
124
+ #####################################################################################################################
125
+ # Functions
126
+ #####################################################################################################################
127
+ #
128
+ # tasks list
129
+ #
130
+ def list_tasks(type)
131
+ current = {:errormsg => "", :status => "", :time => "", :poolstatus => ""}
132
+
133
+ conn_opts=get_cli_connection
134
+ if not type
135
+ Chef::Log.debug("#{conn_opts[:host]}...list tasks")
136
+ Net::SSH.start( conn_opts[:host], conn_opts[:user], :password => conn_opts[:password], :port => conn_opts[:port] ) do|ssh|
137
+ output = ssh.exec!("tasks lists")
138
+ output.each_line do |line|
139
+ if line.match(/Status:/)
140
+ current[:status]=line.split[1].strip
141
+ elsif line.match(/Time:/)
142
+ line["Time: "]=""
143
+ current[:time]=line.strip
144
+ elsif line.match(/ id:/)
145
+ puts line.split(':')[2].strip
146
+ elsif line.match(/Error Msg:/)
147
+ line["Error Msg: "]=""
148
+ current[:errormsg]=line.strip
149
+ end
150
+ end
151
+ end
152
+ return current
153
+ else
154
+ Chef::Log.debug("#{conn_opts[:host]}...list tasks type=#{type}")
155
+ Net::SSH.start( conn_opts[:host], conn_opts[:user], :password => conn_opts[:password], :port => conn_opts[:port] ) do|ssh|
156
+ output = ssh.exec!("tasks list -t #{type}")
157
+ output.each_line do |line|
158
+ if line.match(/Status:/)
159
+ current[:status]=line.split[1].strip
160
+ elsif line.match(/Time:/)
161
+ line["Time: "]=""
162
+ current[:time]=line.strip
163
+ elsif line.match(/Error Msg:/)
164
+ line["Error Msg: "]=""
165
+ current[:errormsg]=line.strip
166
+ elsif line.match(/ /)
167
+ puts line
168
+ end
169
+ end
170
+ end
171
+ return current
172
+ end
173
+ end
174
+
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,32 @@
1
+ #
2
+ # Author:: Michael Huisman (<michhuis@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ require 'chef/knife'
7
+ require 'chef/knife/BaseSmuCommand'
8
+ require 'netaddr'
9
+ require 'net/ssh'
10
+
11
+ # list tasks
12
+ class Chef::Knife::SmucliTasksList < Chef::Knife::BaseSmuCommand
13
+
14
+ banner "knife smucli tasks list <type>"
15
+
16
+ get_common_options
17
+
18
+ def run
19
+
20
+ $stdout.sync = true
21
+
22
+ type = @name_args[0]
23
+
24
+ current=list_tasks(type)
25
+ Chef::Log.debug("Status = #{current[:status]}. Time = #{current[:time]}. VM Status = #{current[:poolstatus]}.")
26
+
27
+ if current[:status]!="Success"
28
+ puts "Call to SMU CLI Failed with #{current[:errormsg]}"
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module KnifeSmuCli
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-smucli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Huisman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: netaddr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: chef
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.0
41
+ description: Oracle SMU Support for Chef's Knife Command
42
+ email: michhuis@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/chef/knife/BaseSmuCommand.rb
48
+ - lib/chef/knife/smucli_tasks_list.rb
49
+ - lib/knife-smucli/version.rb
50
+ homepage: http://github.com/michaelhuisman/knife-smucli
51
+ licenses: []
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.5.1
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Oracle SMU Support for Knife
73
+ test_files: []