Module: AethernalAgent::Utils

Included in:
Apache, Apache, App, Cli::Services, Loader, Webserver::Api, Webserver::Core
Defined in:
lib/aethernal_agent/utils.rb

Instance Method Summary collapse

Instance Method Details

#apt_running?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/aethernal_agent/utils.rb', line 33

def apt_running?
  system("lsof /var/lib/dpkg/lock > /dev/null")
end

#check_manifest(manifest) ⇒ Object



4
5
6
7
8
9
# File 'lib/aethernal_agent/utils.rb', line 4

def check_manifest(manifest)
  if manifest.required_aethernal_agent_version
    AethernalAgent.logger.debug("Checking required aethernal_agent version")
    return Gem::Dependency.new('', manifest.required_aethernal_agent_version).match?('', AethernalAgent::VERSION)
  end
end

#container_domainObject



168
169
170
# File 'lib/aethernal_agent/utils.rb', line 168

def container_domain
  "#{get_global_config(:container_name)}.#{get_global_config(:hostname)}"
end

#current_xdg_runtime_dirObject



172
173
174
# File 'lib/aethernal_agent/utils.rb', line 172

def current_xdg_runtime_dir
  "export XDG_RUNTIME_DIR=/run/user/#{get_current_uid}"
end

#docker_container_nameObject



164
165
166
# File 'lib/aethernal_agent/utils.rb', line 164

def docker_container_name
  "#{self.user}.#{self.manifest.plain_name}"
end

#get_current_uidObject



176
177
178
# File 'lib/aethernal_agent/utils.rb', line 176

def get_current_uid
  `id`.split("(")[0].split("=")[1]
end

#get_global_config(key) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/aethernal_agent/utils.rb', line 108

def get_global_config(key)
  AethernalAgent.logger.debug("Getting value for key '#{key}'")

  begin
    config = JSON.parse(File.read(global_config_path))
  rescue Errno::ENOENT
    config = {}
  end

  AethernalAgent.logger.debug("Value is '#{config[key.to_s]}'")
  return config[key.to_s]
end

#global_config_pathObject



160
161
162
# File 'lib/aethernal_agent/utils.rb', line 160

def global_config_path
  "/etc/aethernal_agent.config"
end

#port_is_free(port) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/aethernal_agent/utils.rb', line 88

def port_is_free(port)
  return false unless port.present?
  begin
    server = TCPServer.new("127.0.0.1", port.to_i)
    server.close
  rescue Errno::EADDRINUSE, Errno::EACCES, TypeError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
    return false
  end
  return true
end

#prepare_test_configObject



154
155
156
157
158
# File 'lib/aethernal_agent/utils.rb', line 154

def prepare_test_config
  File.open(global_config_path, "w") do |f|
    f.write({hostname: "test-server", container_name: "test-container"}.to_json)
  end
end


21
22
23
24
25
# File 'lib/aethernal_agent/utils.rb', line 21

def print_system(command)
  AethernalAgent.logger.warn "pring_system is depcrated"
  AethernalAgent.logger.info "Running command: '#{command}'"
  system(command)
end

#random_port(range = (20000..50000)) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/aethernal_agent/utils.rb', line 99

def random_port(range = (20000..50000))
  port = range.to_a.sample
  if port && port_is_free(port)
    return port.to_i
  else
    random_port(range)
  end
end

#random_string(length = 16) ⇒ Object



27
28
29
30
31
# File 'lib/aethernal_agent/utils.rb', line 27

def random_string(length = 16)
  o =  [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
  string  =  (0...length).map{ o[rand(o.length)] }.join
  return string
end

#run_as(user, command) ⇒ Object



17
18
19
# File 'lib/aethernal_agent/utils.rb', line 17

def run_as(user, command)
  run_command("sudo -iu #{user} #{command}")
end

#run_command(command, options = {}) ⇒ Object



11
12
13
14
15
# File 'lib/aethernal_agent/utils.rb', line 11

def run_command(command, options = {})
  AethernalAgent.logger.info "Running command: '#{command}"
  # TODO: Pipe this to get acutal output of commands to return error messages when stderr is used
  system(command)
end

#run_systemd_plain(action, service_name) ⇒ Object



184
185
186
# File 'lib/aethernal_agent/utils.rb', line 184

def run_systemd_plain(action, service_name)
  `#{current_xdg_runtime_dir} && systemctl --user #{action} #{service_name}`
end

#set_global_config(key, value) ⇒ Object



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
# File 'lib/aethernal_agent/utils.rb', line 121

def set_global_config(key, value)
  AethernalAgent.logger.debug("Setting key '#{key}' to value '#{value}'")

  if File.exists?(global_config_path)
    begin
      config = JSON.parse(File.read(global_config_path))
    rescue StandardError => e
      raise "Could not read/parse global config: '#{e}'"
    end
  else
    AethernalAgent.logger.debug("Config file does not exist yet, creating new.")
    config = {}
  end

  config[key.to_s] = value
  begin
    file = File.open(global_config_path, "w")
    file.write(JSON.dump(config))
    file.close
  rescue StandardError => e
    raise "Could not write out global config file: '#{e}'"
  end

  FileUtils.chown("root","root", global_config_path)
end

#systemd_text_for_service(service_name) ⇒ Object



180
181
182
# File 'lib/aethernal_agent/utils.rb', line 180

def systemd_text_for_service(service_name)
  run_systemd_plain("status", service_name)
end

#ubuntu_releaseObject



147
148
149
150
151
152
# File 'lib/aethernal_agent/utils.rb', line 147

def ubuntu_release
  AethernalAgent.logger.debug("Looking for Linux version number.")
  version_number = %x( lsb_release -sr )
  AethernalAgent.logger.debug("Returning version number: #{version_number.to_s.gsub('.', '')}")
  version_number.to_s.gsub('.', '').strip
end

#wait_on_apt(tries = 120) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aethernal_agent/utils.rb', line 37

def wait_on_apt(tries = 120)
  AethernalAgent.logger.info("Waiting on APT to become ready")

  while apt_running? do
    AethernalAgent.logger.info("APT is still running, sleeping.") if tries % 10 == 0
    tries -= 1
    if tries <= 0
      AethernalAgent.logger.info("Ran out of tries, exiting.")
      return false
    end
    sleep 1
  end

  return true
end

#wait_on_file(path, tries = 20) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aethernal_agent/utils.rb', line 53

def wait_on_file(path, tries = 20)
  AethernalAgent.logger.info("Waiting on file #{path} to exist")

  while !File.exist?(path) do
    AethernalAgent.logger.info("File does not exist yet, sleeping.") if tries % 10 == 0
    tries -= 1
    if tries <= 0
      AethernalAgent.logger.info("Ran out of tries, exiting.")
      return false
    end
    sleep 1
  end

  return true
end

#wait_on_port(port, tries = 120) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aethernal_agent/utils.rb', line 69

def wait_on_port(port, tries = 120)
  AethernalAgent.logger.info("Waiting on port #{port} to be in use.")

  while port_is_free(port) do
    AethernalAgent.logger.info("Port not in use yet, sleeping.") if tries % 10 == 0
    tries -= 1
    if tries <= 0
      AethernalAgent.logger.info("Ran out of tries, exiting.")
      return false
    end
    sleep 1
  end

  # Wait a bit extra to ensure processes are fully loaded
  sleep 2
  return true
end