openshift 0.60.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,188 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright 2010 Red Hat, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person
5
+ # obtaining a copy of this software and associated documentation files
6
+ # (the "Software"), to deal in the Software without restriction,
7
+ # including without limitation the rights to use, copy, modify, merge,
8
+ # publish, distribute, sublicense, and/or sell copies of the Software,
9
+ # and to permit persons to whom the Software is furnished to do so,
10
+ # subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
+ # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
+ # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+
24
+ require 'openshift'
25
+
26
+ def usage
27
+ puts <<USAGE
28
+ == Synopsis
29
+
30
+ os-list-environments: List all environments.
31
+
32
+ == Usage
33
+
34
+ os list-environments [options]
35
+
36
+ -u|--username
37
+ Redhat Login (RHN or OpenShift login with OpenShift Express access)
38
+
39
+ -p|--password
40
+ Redhat Password
41
+
42
+ -t|--target flex|express
43
+ The cloud platform to list environments for.
44
+
45
+ -h|--help
46
+ Prints this message
47
+ USAGE
48
+ end
49
+
50
+ opts = GetoptLong.new(
51
+ ["--username", "-u", GetoptLong::REQUIRED_ARGUMENT],
52
+ ["--password", "-p", GetoptLong::REQUIRED_ARGUMENT],
53
+ ["--target", "-t", GetoptLong::REQUIRED_ARGUMENT],
54
+ ["--help", "-h", GetoptLong::NO_ARGUMENT],
55
+ ["--debug", GetoptLong::NO_ARGUMENT],
56
+ ["--sso", GetoptLong::REQUIRED_ARGUMENT],
57
+ ["--porcelin", GetoptLong::NO_ARGUMENT]
58
+ )
59
+
60
+ args = {}
61
+ begin
62
+ opts.each{ |k,v| args[k]=v }
63
+ rescue GetoptLong::Error => e
64
+ usage
65
+ exit -100
66
+ end
67
+
68
+ if args['--help']
69
+ usage
70
+ exit
71
+ end
72
+
73
+ @debug = true if args['--debug']
74
+ @porcelin = true if args['--porcelin']
75
+
76
+ args['--target'] = conf('default_target') || 'flex' if args['--target'].nil? or args['--target']==""
77
+ debug "Target platform #{args['--target']}"
78
+
79
+ if args['--target'] == 'flex'
80
+ flex_server = conf('flex_server')
81
+
82
+ cookie = args['--sso']
83
+ if !cookie
84
+ username = args['--username'] || conf("username") || Openshift::IO.prompt("Redhat username",[],Openshift::Validation.method(:check_login))
85
+ password = args['--password'] || Openshift::IO.prompt("Redhat password",nil,nil,true,false)
86
+ csay("Logging into Openshift Flex as #{username}\n",:message)
87
+ cookie=Openshift.login(@http,username,password)
88
+ end
89
+
90
+ csay("Contacting Openshift Flex server... ") if not @porcelin
91
+ uri = URI.parse("#{flex_server}/rest/api")
92
+ response = Openshift::Rest.get(@http, uri, nil, cookie, nil)
93
+ case response
94
+ when Net::HTTPSuccess
95
+ csay("[OK]",:conf) if not @porcelin
96
+ else
97
+ debug "HTTP code: #{response.code}"
98
+ debug response.body
99
+ csay("[ERROR]",:error) if not @porcelin
100
+ csay("Unable to contact Flex server",:error)
101
+ exit -301
102
+ end
103
+ data = JSON.parse(response.body)
104
+ list_env_url = data['links']['list-clusters']
105
+
106
+ csay("Retrieving list of environments... ") if not @porcelin
107
+ uri = URI.parse("#{flex_server}/rest/#{list_env_url['href']}")
108
+ response = Openshift::Rest.doHttp(@http, list_env_url['method'], uri, nil, cookie, nil)
109
+ case response
110
+ when Net::HTTPSuccess
111
+ csay("[OK]",:conf) if not @porcelin
112
+ else
113
+ debug "HTTP code: #{response.code}"
114
+ debug response.body
115
+ csay("[ERROR]",:error) if not @porcelin
116
+ csay("Unable to retrieve environment list",:error)
117
+ exit -301
118
+ end
119
+ begin
120
+ data = JSON.parse(response.body)
121
+ data = data['clusters']
122
+ data.each{ |c|
123
+ c['num-nodes'] = c['nodes'].size
124
+ }
125
+ if args['--porcelin']
126
+ print JSON.generate(data)
127
+ else
128
+ Openshift::Formatter.table(["Environment Id","Name","Cloud", "DNS", "Load balanced", "Location", "State"],
129
+ ['id','name','cloud-account-name', 'dns', 'loadbalanced','location','cluster-status'],
130
+ [16,15,20,40,20,15,10],
131
+ data)
132
+ end
133
+ end
134
+ else
135
+ sso_data = args['--sso']
136
+ if !sso_data
137
+ username = args['--username'] || conf("username") || Openshift::IO.prompt("Redhat username",[],Openshift::Validation.method(:check_login))
138
+ password = args['--password'] || Openshift::IO.prompt("Redhat password",nil,nil,true,false)
139
+ sso_data = JSON.generate({"username"=>username, "password"=>password})
140
+ else
141
+ sso_data = JSON.parse(sso_data)
142
+ username = sso_data['username']
143
+ password = sso_data['password']
144
+ end
145
+
146
+ data = {'rhlogin' => username}
147
+ data['debug'] = "true"
148
+ Openshift::Debug.print_post_data(data)
149
+ json_data = JSON.generate(data)
150
+
151
+ url = URI.parse("https://#{libra_server}/app/broker/userinfo")
152
+ csay("Retrieving list of environments... ") if not @porcelin
153
+ response = Openshift::Rest.post(net_http, url, {"json_data" => json_data, "password" => password})
154
+ case response
155
+ when Net::HTTPSuccess
156
+ csay("[OK]",:conf) if not @porcelin
157
+ when Net::HTTPNotFound
158
+ csay("No Openshift Express environments found for user '#{username}'")
159
+ csay("Be sure to run os-create-environment before using the other os tools.")
160
+ exit -200
161
+ when Net::HTTPUnauthorized
162
+ csay("Invalid login credentials.")
163
+ exit -401
164
+ else
165
+ debug "HTTP code: #{response.code}"
166
+ debug response.body
167
+ csay("[ERROR]",:error) if not @porcelin
168
+ csay("Unable to retrieve environment list",:error)
169
+ exit -301
170
+ end
171
+
172
+ begin
173
+ json_resp = JSON.parse(response.body)
174
+ user_info = JSON.parse(json_resp['result'].to_s)
175
+ rescue JSON::ParserError
176
+ exit -400
177
+ end
178
+
179
+ if @porcelin
180
+ print user_info
181
+ else
182
+ csay("User Info",:message)
183
+ csay("Namespace: #{user_info['user_info']['namespace']}")
184
+ csay(" UUID: #{user_info['user_info']['uuid']}")
185
+ csay(" RHLogin: #{user_info['user_info']['rhlogin']}")
186
+ csay(" ssh_key: #{user_info['user_info']['ssh_key']}")
187
+ end
188
+ end
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright 2010 Red Hat, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person
5
+ # obtaining a copy of this software and associated documentation files
6
+ # (the "Software"), to deal in the Software without restriction,
7
+ # including without limitation the rights to use, copy, modify, merge,
8
+ # publish, distribute, sublicense, and/or sell copies of the Software,
9
+ # and to permit persons to whom the Software is furnished to do so,
10
+ # subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
+ # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
+ # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+
24
+ require 'openshift'
25
+
26
+ def usage
27
+ puts <<USAGE
28
+ == Synopsis
29
+
30
+ rhc-list-servers: List all servers on all running environments.
31
+
32
+ == Usage
33
+
34
+ rhc list-servers [options]
35
+
36
+ -u|--username
37
+ Redhat Login (RHN or OpenShift login with OpenShift Express access)
38
+
39
+ -p|--password
40
+ Redhat Password
41
+
42
+ -t|--target flex|express
43
+ The cloud platform hosting the environments.
44
+
45
+ -h|--help
46
+ Prints this message
47
+ USAGE
48
+ end
49
+
50
+ opts = GetoptLong.new(
51
+ ["--username", "-u", GetoptLong::REQUIRED_ARGUMENT],
52
+ ["--password", "-p", GetoptLong::REQUIRED_ARGUMENT],
53
+ ["--target", "-t", GetoptLong::REQUIRED_ARGUMENT],
54
+ ["--sso", GetoptLong::REQUIRED_ARGUMENT],
55
+ ["--porcelin", GetoptLong::NO_ARGUMENT],
56
+ ["--debug", GetoptLong::NO_ARGUMENT]
57
+ )
58
+
59
+ args = {}
60
+ begin
61
+ opts.each{ |k,v| args[k]=v }
62
+ rescue GetoptLong::Error => e
63
+ usage
64
+ exit -100
65
+ end
66
+
67
+ if args['--help']
68
+ usage
69
+ exit
70
+ end
71
+
72
+ @debug = true if args['--debug']
73
+ @porcelin = true if args['--porcelin']
74
+
75
+ args['--target'] = conf('default_target') || 'flex' if args['--target'].nil? or args['--target']==""
76
+ debug "Target platform #{args['--target']}"
77
+
78
+ if args['--target'] == 'flex'
79
+ flex_server = conf('flex_server')
80
+
81
+ cookie = args['--sso']
82
+ if !cookie
83
+ username = args['--username'] || conf("username") || Openshift::IO.prompt("Redhat username",[],Openshift::Validation.method(:check_login))
84
+ password = args['--password'] || Openshift::IO.prompt("Redhat password",nil,nil,true,false)
85
+ csay("Logging into Openshift Flex as #{username}\n",:message)
86
+ cookie=Openshift.login(@http,username,password)
87
+ end
88
+
89
+ csay("Retrieving list of environments... ") if not @porcelin
90
+ uri = URI.parse("#{flex_server}/rest/clusters")
91
+ response = Openshift::Rest.get(@http, uri, nil, cookie, nil)
92
+ case response
93
+ when Net::HTTPSuccess
94
+ csay("[OK]",:conf) if not @porcelin
95
+ else
96
+ debug "HTTP code: #{response.code}"
97
+ debug response.body
98
+ csay("[ERROR]",:error) if not @porcelin
99
+ csay("Unable to retrieve environment list",:error)
100
+ exit -301
101
+ end
102
+
103
+ data = JSON.parse(response.body)
104
+ data = data['clusters']
105
+
106
+ data.each{ |environment|
107
+ # if not environment_name or environment["name"].strip == environment_name.strip
108
+ Openshift::Formatter.table(["Environment Id","Name","Cloud", "DNS", "Load balancer", "Location", "State"],
109
+ ['id','name','cloud-account-name', 'dns', 'load-balancer-address','location','cluster-status'],
110
+ [16,15,20,40,20,15,10],
111
+ [environment])
112
+ debug environment['nodes']
113
+ csay("Servers",:message)
114
+ Openshift::Formatter.table(["Name","Node Provider Id","IP Address","Date Created","State"],
115
+ ['name','provider-id', 'ip-address','creation-date','status'],
116
+ [15,25,20,15,10],
117
+ environment["nodes"], 1)
118
+ # end
119
+ }
120
+ else
121
+ csay("This feature is currently not implemented for Openshift Express applications.\n",:red)
122
+ end
@@ -0,0 +1,162 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright 2010 Red Hat, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person
5
+ # obtaining a copy of this software and associated documentation files
6
+ # (the "Software"), to deal in the Software without restriction,
7
+ # including without limitation the rights to use, copy, modify, merge,
8
+ # publish, distribute, sublicense, and/or sell copies of the Software,
9
+ # and to permit persons to whom the Software is furnished to do so,
10
+ # subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
+ # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
+ # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+
24
+ require 'openshift'
25
+
26
+ def usage
27
+ puts <<USAGE
28
+ == Synopsis
29
+
30
+ os-open-console: Open shell access to an environment.
31
+
32
+ == Usage
33
+
34
+ os open-console [options] [ENV]
35
+
36
+ -u|--username
37
+ Redhat Login (RHN or OpenShift login with OpenShift Express access)
38
+
39
+ -p|--password
40
+ Redhat Password
41
+
42
+ -t|--target flex|express
43
+ The cloud platform hosting the environments.
44
+
45
+ -h|--help
46
+ Prints this message
47
+
48
+ ENV: The name or GUID of the environment to open a shell to.
49
+ USAGE
50
+ end
51
+
52
+ opts = GetoptLong.new(
53
+ ["--username", "-u", GetoptLong::REQUIRED_ARGUMENT],
54
+ ["--password", "-p", GetoptLong::REQUIRED_ARGUMENT],
55
+ ["--help", "-h", GetoptLong::NO_ARGUMENT],
56
+ ["--target", "-t", GetoptLong::REQUIRED_ARGUMENT],
57
+ ["--debug", GetoptLong::NO_ARGUMENT],
58
+ ["--sso", GetoptLong::REQUIRED_ARGUMENT]
59
+ )
60
+
61
+ args = {}
62
+ begin
63
+ opts.each{ |k,v| args[k]=v }
64
+ rescue GetoptLong::Error => e
65
+ usage
66
+ exit -100
67
+ end
68
+
69
+ if args['--help']
70
+ usage
71
+ exit
72
+ end
73
+
74
+ @debug = true if args['--debug']
75
+ args['--target'] = conf('default_target') || 'flex' if args['--target'].nil? or args['--target']==""
76
+ debug "Target platform #{args['--target']}"
77
+
78
+ if args['--target'] == 'flex'
79
+ flex_server = conf('flex_server')
80
+
81
+ cookie = args['--sso']
82
+ if !cookie
83
+ username = args['--username'] || conf("username") || Openshift::IO.prompt("Redhat username",[],Openshift::Validation.method(:check_login))
84
+ password = args['--password'] || Openshift::IO.prompt("Redhat password",nil,nil,true,false)
85
+ csay("Logging into Openshift Flex as #{username}\n",:message)
86
+ cookie=Openshift.login(@http,username,password)
87
+ end
88
+
89
+ environment_id = ARGV.shift
90
+ if not environment_id
91
+ csay("Environment name or GUID not specified",:error)
92
+ exit -101
93
+ end
94
+
95
+ begin
96
+ csay("Retrieving list of environments... ")
97
+ environments = `os-list-environments --sso "#{cookie}" --porcelin`
98
+ environments = JSON.parse(environments)
99
+ csay("[OK]",:conf)
100
+ rescue Exception => e
101
+ debug environments
102
+ debug e.message
103
+ csay("[ERROR]",:error)
104
+ csay("Unable to retrieve environment list",:error)
105
+ exit -400
106
+ end
107
+ candidates = environments.find_all{ |c| c["name"]==environment_id or c["id"]==environment_id }
108
+
109
+ if candidates.size == 0
110
+ csay("Unable to find environment identified by #{environment_id}",:error)
111
+ exit -200
112
+ end
113
+
114
+ if candidates.size > 1
115
+ csay("Multiple environments are named #{environment_id}. Please provide the environment Id",:error)
116
+ exit -201
117
+ end
118
+
119
+ environment = candidates[0]
120
+ if not File.readable?(@libra_kfile)
121
+ csay("Generating OpenShift Express ssh key to #{@libra_kfile}",:message)
122
+ debug "Invoking ssh-keygen -t rsa -f '#{@libra_kfile}'"
123
+ system("ssh-keygen -t rsa -f '#{@libra_kfile}'")
124
+ end
125
+ ssh_key = File.open(@libra_kpfile).gets.chomp.split(' ')[1]
126
+
127
+ csay("\nRetrieving environment SSH keys... ")
128
+ uri = URI.parse("https://#{environment['dns']}:4242/security/keys")
129
+ response = Openshift::Rest.get(@http, uri, nil, nil, {'user' => environment['username'], 'password' => environment['password']})
130
+ case response
131
+ when Net::HTTPSuccess
132
+ csay("[OK]",:conf)
133
+ else
134
+ debug "HTTP code: #{response.code}"
135
+ debug response.body
136
+ csay("[ERROR]",:error)
137
+ csay("Unable to retrieve environment ssh-keys. Message: #{data['error']}",:error)
138
+ exit -301
139
+ end
140
+
141
+ data = nil
142
+ data = JSON.parse(response.body)
143
+ keys = data['keys']
144
+ if not keys.index(ssh_key)
145
+ csay("\nUploading ssh-key to environment... ")
146
+ uri = URI.parse("https://#{environment['dns']}:4242/security/keys")
147
+ response = Openshift::Rest.post(@http, uri, {'type'=>'ssh-rsa','identifier'=>'', 'key'=>ssh_key}, nil, {'user' => environment['username'], 'password' => environment['password']})
148
+ case response
149
+ when Net::HTTPSuccess
150
+ csay("[OK]",:conf)
151
+ else
152
+ debug "HTTP code: #{response.code}"
153
+ debug response.body
154
+ csay("[ERROR]",:error)
155
+ csay("Unable to update ssh keys on environment. Please use environment admin password when prompted below",:error)
156
+ end
157
+ end
158
+
159
+ system( "ssh admin@#{environment['dns']}" )
160
+ else
161
+ csay("This feature is currently not implemented for Openshift Express applications.\n",:red)
162
+ end