Class: Rncher::Service

Inherits:
Thor
  • Object
show all
Defined in:
lib/rncher/service.rb

Instance Method Summary collapse

Instance Method Details

#execObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rncher/service.rb', line 35

def exec
  app = App.where({system: false, name: options[:app]}).first
  if app
    service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
    container_id = service.instanceIds.first
    container = Container.find(container_id)
    credential = container.remote_token(options[:command])
    connection = RemoteConnection.new(credential[:url], credential[:token])
    connection.start
  end
end

#listObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rncher/service.rb', line 6

def list
  app = App.where({system: false, name: options[:app]}).first
  if app
    services = Service.where({system: false, stackId: app.id})
    table = TTY::Table.new header: ['name','state', 'health', 'scale']
    services.each do |app|
      table <<  [app.name, app.state, app.healthState, app.scale]
    end
    puts table.render(:ascii, border:{separator: :each_row})
  end
end

#logsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rncher/service.rb', line 50

def logs
  app = App.where({system: false, name: options[:app]}).first
  if app
    service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
    EventMachine.run do
      service.instanceIds.each do |container_id|
        container = Container.find(container_id)
        credential = container.logs_token
        connection = RemoteConnection.new(credential[:url], credential[:token])
        connection.start_logs(container)
      end
    end
  end
end

#scale(num) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/rncher/service.rb', line 21

def scale(num)
  app = App.where({system: false, name: options[:app]}).first
  if app
    service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
    service.scale = num
    service.save
    puts 'Done'
  end
end

#statusObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rncher/service.rb', line 68

def status
  app = App.where({system: false, name: options[:app]}).first
  if app
    service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
    EventMachine.run do
      credential = service.request_token
      connection = RemoteConnection.new(credential[:url], credential[:token])
      connection.start_status(service)
    end
  end
end

#upgradeObject



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
# File 'lib/rncher/service.rb', line 86

def upgrade
  app = App.where({system: false, name: options[:app]}).first
  if app
    service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
    launch_config = service.launchConfig
    if options[:image]
      launch_config.imageUuid = "docker:#{options[:image]}"
    end
    if options[:vars]
      new_configs = Hash.new
      options[:vars].map{|k, v| new_configs[k.upcase] = v}
      new_attributes = launch_config.environment.attributes.merge(new_configs)
      launch_config.environment.attributes = new_attributes
    end
    if options[:remove_vars]
      new_configs = Hash.new
      launch_config.environment.attributes.each do |k, v|
        new_configs[k.upcase] = v unless options[:remove_vars].collect{|c| c.upcase}.include?(k.upcase)
      end
      launch_config.environment.attributes = new_configs
    end
    if options[:image] || options[:vars] || options[:remove_vars]
      attributes = {
      	"inServiceStrategy": {
      		"batchSize": 1,
      		"intervalMillis": 2000,
          "launchConfig": launch_config,
      		"startFirst": true
      	},
        "toServiceStrategy": "null"
      }
      service.upgrade(attributes)
      service = Service.find(service.id)
      state = service.state
      while(state=='upgrading') do
        sleep 2
        service = Service.find(service.id)
        state = service.state
      end
      service.finish_upgrade
    end
  end
end