Module: Qtc::Cli::Common

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#datacenter_idObject

Returns the value of attribute datacenter_id.



6
7
8
# File 'lib/qtc/cli/common.rb', line 6

def datacenter_id
  @datacenter_id
end

Instance Method Details

#clientQtc::Client

Returns:



131
132
133
134
135
136
137
# File 'lib/qtc/cli/common.rb', line 131

def client
  if @client.nil?
    @client = Qtc::Client.new(base_url)
  end

  @client
end

#current_cloud_dcString

Returns:

  • (String)


34
35
36
37
38
39
40
41
42
# File 'lib/qtc/cli/common.rb', line 34

def current_cloud_dc
  unless @current_cloud_dc
    unless inifile['platform']['current_cloud']
      raise ArgumentError.new("Please specify used cloud first: qtc-cli clouds:use <id>")
    end
    @current_cloud_dc = inifile['platform']['current_dc']
  end
  @current_cloud_dc
end

#current_cloud_idString

Returns:

  • (String)


21
22
23
24
25
26
27
28
29
30
# File 'lib/qtc/cli/common.rb', line 21

def current_cloud_id
  unless @current_cloud_id
    unless inifile['platform']['current_cloud']
      raise ArgumentError.new("Please specify used cloud first: qtc-cli clouds:use <id>")
    end
    @current_cloud_id = inifile['platform']['current_cloud']
    self.datacenter_id = inifile['platform']['current_dc']
  end
  @current_cloud_id
end

#current_cloud_tokenString

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/qtc/cli/common.rb', line 46

def current_cloud_token
  token = nil
  begin
    authorizations = platform_client.get("/accounts/#{current_cloud_id}/authorizations")
    unless authorizations['results'][0]
      platform_client.post("/accounts/#{current_cloud_id}/authorizations", {})
      raise StandardError.new "retry"
    end
    token = authorizations['results'][0]['access_token']
  rescue ArgumentError => e
    raise e
  rescue Qtc::Errors::StandardError => e
    if e.status == 404
      raise ArgumentError.new("Cloud not found. Please specify used cloud first: qtc-cli clouds:use <id>")
    end
  rescue => e
    retry
  end

  token
end

#extract_app_in_dir(remote = nil) ⇒ String

Parameters:

  • (String, NilClass)

Returns:

  • (String)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/qtc/cli/common.rb', line 111

def extract_app_in_dir(remote = nil)
  if File.exists?(File.expand_path('./.git/config'))
    remotes = []
    git_remotes = `git remote -v`
    git_remotes.lines.each do |line|
      if match = line.match(/#{remote}\s+git@git-mar-(.*)\:(.*) \(push\)/)
        remotes << match[2]
      end
    end
    apps = remotes.uniq
    if apps.size == 1
      return apps[0]
    elsif apps.size > 1
      raise ArgumentError.new("Multiple app git remotes\nSpecify app with --remote REMOTE or --app APP")
    end
  end
end

#ini_filenameString

Returns:

  • (String)


86
87
88
# File 'lib/qtc/cli/common.rb', line 86

def ini_filename
  File.join(Dir.home, '/.qtc_client')
end

#inifileHash

Returns:

  • (Hash)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/qtc/cli/common.rb', line 92

def inifile
  if @inifile.nil?
    if File.exists?(ini_filename)
      @inifile = IniFile.load(ini_filename)
    else
      @inifile = IniFile.new
    end
  end

  unless @inifile['platform']
    @inifile['platform'] = {}
  end

  @inifile
end

#instance_info(instance_id) ⇒ Object

Parameters:

  • instance_id (String)


10
11
12
13
14
15
16
17
# File 'lib/qtc/cli/common.rb', line 10

def instance_info(instance_id)
  instance_data = platform_client.get("/instances/#{instance_id}")
  unless instance_data
    abort("Error: instance not found")
  end

  instance_data
end

#platform_base_urlString

Returns:

  • (String)


141
142
143
# File 'lib/qtc/cli/common.rb', line 141

def platform_base_url
  ENV['QTC_PLATFORM_URL'] || 'https://api.qtc.io/v1'
end

#platform_client(token = nil) ⇒ Qtc::Client

Parameters:

  • token (String) (defaults to: nil)

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/qtc/cli/common.rb', line 71

def platform_client(token = nil)
  inifile['platform']['token'] = token unless token.nil?
  unless inifile['platform']['token']
    raise ArgumentError.new("Please login first using: qtc-cli login")
  end

  if @platform_client.nil?
    @platform_client = Qtc::Client.new(platform_base_url, {'Authorization' => "Bearer #{inifile['platform']['token']}"})
  end

  @platform_client
end