Class: OcpCommander

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/commander/OcpCommander.rb

Instance Method Summary collapse

Instance Method Details

#runObject

include whatever modules you need



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
194
195
196
197
198
199
200
201
202
203
204
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/commander/OcpCommander.rb', line 24

def run

  program :name, 'ocp'
  program :version, '0.0.1'
  program :description, 'A Ruby command line tool to interact with OpenShift Container Platform'

  global_option('-t','--token [TOKEN]','Provide a token.  This option will over-ride what is in a config file')
  global_option('--clientcertfile [CLIENTCERT]','Provide a path to the client certificate file.  This option will over-ride what is in a config file')
  global_option('--clientkeyfile [CLIENTKEY]','Provide a path to the client key file.  This option will over-ride what is in a config file')
  global_option('--clientcafile [CLIENTCA]','Provide a path to the client CA certificate file.  This option will over-ride what is in a config file')
  global_option('-c','--config [CONFIG]','Provide a config file.  See README.md for an example')
  global_option('-m','--master [MASTER]','OpenShift Cluster to which wish to connect.  This option will over-ride what is in a config file')
  global_option('-n','--noverifyssl','Do not verify SSL certificate of master.  This option will over-ride what is in a config file')
  global_option('-o','--output [OUTPUT]',[:yaml, :json], "Select the output format")
  global_option('-d','--debug', "Dump debug information to console")
  global_option('-p','--pretty', "Generate pretty output where possible")

  command :getocpapi do |c|
    c.syntax = 'ocp getocpapi [options]'
    c.description = 'Retrieve the OpenShift Container Platform API'
    c.action do |args, options|

      ocpapi = OcpApi.new
      ocpapi.setup_by_config_file(options.config)
      ocpapi.list
      puts "#{ocpapi.response}"
    end
  end

  command :getapi do |c|
    c.syntax = 'ocp getapi [options]'
    c.summary = 'Retrieve the Kubernetes API'
    c.description = ''
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      api = Api.new
      api.setup_by_config_file(options.config)
      puts "#{api.showapi}"
    end
  end

  command :projectexists do |c|
    c.syntax = 'ocp projectexists [options]'
    c.summary = 'Check if a project exists'
    c.description = 'Check if a project exists'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      proj = Project.new
      proj.setup_by_config_file(options.config, options.pretty, options.debug)

      if proj.exists?(options.name)
        puts "Project #{options.name} exists"
      else
        puts "Project #{options.name} doesn't exist"
      end

    end
  end

  command :listprojects do |c|
    c.syntax = 'ocp projectexists [options]'
    c.summary = 'Check if a project exists'
    c.description = 'Check if a project exists'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      proj = Project.new
      proj.setup_by_config_file(options.config, options.pretty, options.debug)

      puts "#{proj.listprojects}"

    end
  end

  command :list_projects_by_user do |c|
    c.syntax = 'ocp projectexists [options]'
    c.summary = 'List projects by user'
    c.description = 'List projects by user'
    c.option '--username USERNAME', String, 'The user to search by'
    c.option '--role ROLE', String, 'The optional role to filter by'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      valid_project_list = Array.new
      proj = Project.new
      proj.setup_by_config_file(options.config, options.pretty, options.debug)

      proj.list_projects_array.each do |projname|
        role_bindings = RoleBinding.new(projname)

        bindings_list = JSON.parse role_bindings.to_s
        list = bindings_list['items']
        list.each do |key|

          unless options.role.nil?
            if key['metadata']['name'].eql? options.role and !key['userNames'].nil? and key['userNames'].include? options.username
              valid_project_list.push projname
            end
          else
            if !key['userNames'].nil? and key['userNames'].include? options.username
              valid_project_list.push projname
            end
          end
        end

      end

      puts "#{valid_project_list}"

    end
  end

  command :createproject do |c|
    c.syntax = 'ocp createproject [options]'
    c.summary = 'Create a project request'
    c.description = 'Create a project request'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.option '--description [DESCRIPTION]', String, 'The description for the project'
    c.option '--displayname [DISPLAYNAME]', String, 'The displayname for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      projreq = ProjectRequest.new
      projreq.setup_by_config_file(options.config)

      data = projreq.createprojectrequest(options.name, options.description, options.displayname)

      puts "#{data}"
    end
  end

  command :deleteproject do |c|
    c.syntax = 'ocp deleteproject [options]'
    c.summary = 'Delete a project'
    c.description = 'Delete a project'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      project = Project.new
      project.setup_by_config_file(options.config)

      data = project.deleteproject(options.name)

      puts "#{data}"
    end
  end

  command :listpolicies do |c|
    c.syntax = 'ocp listpolicies [options]'
    c.description = 'Retrieve the Policies for a NameSpace'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      policy = Policy.new
      policy.setup_by_config_file(options.config)
      puts "#{policy}"
    end
  end

  command :listoauthclientauthorizations do |c|
    c.syntax = 'ocp oauthclientauthorizations [options]'
    c.description = 'Retrieve the OAuthClientAuthorizations'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      oauthpolicy = OAuthClientAuthorization.new
      oauthpolicy.setup_by_config_file(options.config)
      puts "#{oauthpolicy}"
    end
  end

  command :listroles do |c|
    c.syntax = 'ocp listroles [options]'
    c.description = 'Retrieve the Roles'
    c.option '--name [PROJECTNAME]', String, 'Optionally the name of the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      roles = nil
      if options.name.nil?
        roles = Role.new
      else
        roles = Role.new(options.name)
      end
      roles.setup_by_config_file(options.config)

      puts "#{roles}"
    end
  end

  command :listrolebindings do |c|
    c.syntax = 'ocp listrolebindings [options]'
    c.description = 'Retrieve the Roles Bindings'
    c.option '--name [PROJECTNAME]', String, 'Optionally the name of the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      roles = nil
      if options.name.nil?
        roles = RoleBinding.new
      else
        roles = RoleBinding.new(options.name)
      end
      roles.setup_by_config_file(options.config, options.pretty, options.debug)


      puts "#{roles}"
    end
  end

  command :create_role_binding do |c|
    c.syntax = 'ocp createrolebinding [options]'
    c.description = 'Create a Project Roles Binding'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.option '--user USERNAME', String, 'The name of the user'
    c.option '--role ROLE', String, 'The role to add to the user'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      roles = RoleBinding.new(options.name)

      roles.setup_by_config_file(options.config, options.pretty, options.debug)
      resp = roles.create_role_binding(options.user, options.role)
      puts "#{resp}"

    end
  end

  command :update_role_binding do |c|
    c.syntax = 'ocp update_role_binding [options]'
    c.description = 'Update a Project Roles Binding'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.option '--user USERNAME', String, 'The name of the user'
    c.option '--role ROLE', String, 'The role to add to the user'
    c.action do |args, options|

      roles = RoleBinding.new(options.name)

      roles.setup_by_config_file(options.config, options.pretty, options.debug)
      resp = roles.update_role_binding(options.user, options.role)
      puts "#{resp}"

    end
  end

  command :list_resource_quotas do |c|
    c.syntax = 'ocp list_resource_quotas [options]'
    c.description = 'List a Project\'s Resource Quotas'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      rq = ResourceQuota.new(options.name)

      rq.setup_by_config_file(options.config, options.pretty, options.debug)
      resp = rq.list_resource_quotas
      puts "#{resp}"

    end
  end

  command :create_resource_quota do |c|
    c.syntax = 'ocp create_resource_quota [options]'
    c.description = 'Create a Project\'s Resource Quota'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.option '--pods PODS', Integer, 'The number of pods'
    c.option '--replicationcontrollers RCS', Integer, 'The number of replication controllers'
    c.option '--services SERVICES', Integer, 'The number of services'
    c.option '--configmaps CONFIGMAPS', Integer, 'The number of configmaps'
    c.option '--persistentvolumeclaims PVCS', Integer, 'The number of persistent volume claims'
    c.option '--resourcequotas RQS', Integer, 'The number of resource quotas'
    c.action do |args, options|
      rq = ResourceQuota.new(options.name)

      rq.setup_by_config_file(options.config, options.pretty, options.debug)
      resp = rq.create_resource_quota(options.name, options.pods,
                  options.replicationcontrollers, options.services,
                  options.configmaps, options.persistentvolumeclaims, options.resourcequotas)
      puts "#{resp}"
    end
  end
  command :delete_resource_quota do |c|
    c.syntax = 'ocp delete_resource_quota [options]'
    c.description = 'Delete a Project\'s Resource Quota'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.option '--quotaname QUOTANAME', String, 'The name of the quota object'
    c.action do |args, options|
      rq = ResourceQuota.new(options.name)

      rq.setup_by_config_file(options.config)
      resp = rq.delete_resource_quota(options.quotaname)
      puts "#{resp}"

    end
  end



  run!
end