Class: Xplenty::Kensa::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/xplenty/kensa/client.rb

Defined Under Namespace

Classes: CommandInvalid, OptParser, Screen

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
# File 'lib/xplenty/kensa/client.rb', line 11

def initialize(args, options = {})
  @args    = args
  @options = OptParser.parse(args).merge(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/xplenty/kensa/client.rb', line 9

def options
  @options
end

Instance Method Details

#createObject

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/xplenty/kensa/client.rb', line 34

def create
  app_name = @args.shift
  template = @options[:template]
  raise CommandInvalid.new("Need to supply an application name") unless app_name
  raise CommandInvalid.new("Need to supply a template") unless template
  begin
    Git.clone(app_name, template) and screen.message "Created #{app_name} from #{template} template\n"
    Dir.chdir(app_name)
    @options[:foreman] = true
    init
  rescue Exception => e
    raise CommandInvalid.new("error cloning #{Git.clone_url(template)} into #{app_name}") 
  end
end

#initObject



24
25
26
27
28
29
30
31
32
# File 'lib/xplenty/kensa/client.rb', line 24

def init
  manifest = Manifest.new(@options)
  protect_current_manifest!
  manifest.write
  screen.message "Initialized new addon manifest in #{filename}\n" 
  if @options[:foreman]
    screen.message "Initialized new .env file for foreman\n"
  end
end

#pullObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/xplenty/kensa/client.rb', line 104

def pull
  addon = @args.first || abort('usage: kensa pull <add-on name>')
  protect_current_manifest!

  user, password = ask_for_credentials
  host     = xplenty_host
  resource = RestClient::Resource.new(host, user, password)
  manifest = resource["provider/addons/#{addon}"].get(headers)
  File.open(filename, 'w') { |f| f.puts manifest }
  puts "-----> Manifest for \"#{addon}\" received successfully"
end

#pushObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xplenty/kensa/client.rb', line 88

def push
  user, password = ask_for_credentials
  host     = xplenty_host
  data     = decoded_manifest
  resource = RestClient::Resource.new(host, user, password)
  resource['provider/addons'].post(resolve_manifest, headers)
  puts "-----> Manifest for \"#{data['id']}\" was pushed successfully"
  puts "       Continue at #{(xplenty_host)}/provider/addons/#{data['id']}"
rescue RestClient::UnprocessableEntity, RestClient::BadRequest => e
  abort("FAILED: #{e.response}")
rescue RestClient::Unauthorized
  abort("Authentication failure")
rescue RestClient::Forbidden
  abort("Not authorized to push this manifest. Please make sure you have permissions to push it")
end

#runObject



74
75
76
77
78
# File 'lib/xplenty/kensa/client.rb', line 74

def run
  abort "! missing command to run; see usage" if @args.empty?
  run_check ManifestCheck
  run_check AllCheck, :args => @args
end

#run!Object

Raises:



18
19
20
21
22
# File 'lib/xplenty/kensa/client.rb', line 18

def run!
  command = @args.shift || @options[:command]
  raise CommandInvalid unless command && respond_to?(command)
  send(command)
end

#ssoObject



80
81
82
83
84
85
86
# File 'lib/xplenty/kensa/client.rb', line 80

def sso
  id = @args.shift || abort("! no id specified; see usage")
  data = decoded_manifest
  sso = Sso.new(data.merge(@options).merge(:id => id)).start
  puts sso.message
  Launchy.open sso.sso_url
end

#testObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xplenty/kensa/client.rb', line 49

def test
  case check = @args.shift
    when "manifest"
      run_check ManifestCheck
    when "provision"
      run_check ManifestCheck, ProvisionCheck
    when "deprovision"
      id = @args.shift || abort("! no id specified; see usage")
      run_check ManifestCheck, DeprovisionCheck, :id => id
    when "planchange"
      id   = @args.shift || abort("! no id specified; see usage")
      plan = @args.shift || abort("! no plan specified; see usage")
      run_check ManifestCheck, PlanChangeCheck, :id => id, :plan => plan
    when "sso"
      id = @args.shift || abort("! no id specified; see usage")
      run_check ManifestCheck, SsoCheck, :id => id
    when "all"
      run_check AllCheck
    when nil
      run_check AllCheck
    else
      abort "! Unknown test '#{check}'; see usage"
  end
end

#versionObject



116
117
118
# File 'lib/xplenty/kensa/client.rb', line 116

def version
  puts "Kensa #{VERSION}"
end