Module: DockerMgr::Util

Included in:
BackupApp, CommandRouter::Main, Create, Delete, Generate, Increment, Init, RestoreApp, Start
Defined in:
lib/util.rb

Instance Method Summary collapse

Instance Method Details

#add_line_to_routine(routine, line) ⇒ Object



136
137
138
# File 'lib/util.rb', line 136

def add_line_to_routine(routine,line)
  File.open("#{routine_dir}/#{routine}",'a') { | file  | file.write("#{line}\n") }
end

#add_packages(path, *packages) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/util.rb', line 174

def add_packages(path,*packages) 
  packages.each_with_index do |package,index|
    FileUtils.cp("#{install_dir}/install_#{package}.sh", 
                 "#{path}/administration/installation/#{index}_install_#{package}.sh")
  end
  FileUtils.cp("#{install_dir}/scriptrunner.sh", 
               "#{path}/administration/scriptrunner.sh")
end

#add_trust(path) ⇒ Object



182
183
184
185
186
# File 'lib/util.rb', line 182

def add_trust(path)
  FileUtils.cp("#{admin_dir}/trust.sh", 
               "#{path}/administration/trust.sh")
  empty_directory("#{path}/administration/certificates")
end

#admin_dirObject



48
49
50
# File 'lib/util.rb', line 48

def admin_dir
  "#{root_dir}/admin"
end

#apps_dirObject



44
45
46
# File 'lib/util.rb', line 44

def apps_dir
  "#{root_dir}/apps"
end

#attic_dirObject



40
41
42
# File 'lib/util.rb', line 40

def attic_dir
  "#{root_dir}/attic"
end

#backup_dirObject



27
28
29
# File 'lib/util.rb', line 27

def backup_dir
  "#{root_dir}/backup" 
end

#base_images_dirObject



31
32
33
# File 'lib/util.rb', line 31

def base_images_dir
  "#{root_dir}/base_images"
end

#cert_dirObject



66
67
68
# File 'lib/util.rb', line 66

def cert_dir
  "#{proxy_dir}/ca_certs"
end

#configObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/util.rb', line 74

def config 
  if File.exist? "#{admin_dir}/config.yml"
    YAML.load_file "#{admin_dir}/config.yml"
  else
    result = Hash.new
    result[:email] = extract_email
    result[:name] = extract_name
    host = "#{result[:name].gsub(/\s/,'-').downcase}.de"
    puts "pleas enter your host-name (#{host})"
    choice = STDIN.gets.chomp
    result[:host] = choice.empty? ? host : choice
    File.write "#{admin_dir}/config.yml", result.to_yaml
    result
  end
end

#data_services(app_name) ⇒ Object



106
107
108
109
# File 'lib/util.rb', line 106

def data_services(app_name)
  YAML.load(File.read("#{apps_dir}/#{app_name}/docker-compose.yml")).each_key
    .select {|k| k.end_with?("data")}
end

#exec_hook(app_name, type, hook_name) ⇒ Object



120
121
122
123
124
# File 'lib/util.rb', line 120

def exec_hook(app_name,type,hook_name)
  if File.exist? "#{apps_dir}/#{app_name}/administration/hooks/#{type}.d/#{hook_name}"
    `#{apps_dir}/#{app_name}/administration/hooks/#{type}.d/#{hook_name}`
  end
end

#extract_date(entry) ⇒ Object



91
92
93
# File 'lib/util.rb', line 91

def extract_date(entry)
  /_\d+\./.match(entry).to_s.chop[1..-1].to_i
end

#extract_emailObject



151
152
153
# File 'lib/util.rb', line 151

def extract_email
  extract_git_variable("user.email")
end

#extract_git_variable(name) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/util.rb', line 141

def extract_git_variable(name)
  git_config = `git config --list`
  result = git_config.lines.grep(/#{Regexp.quote(name)}/).map{|e| e.split('=')[1].chomp }.first
  unless result
    puts "please enter your #{name.split('.')[1]}"
    result = STDIN.gets.chomp
  end
  result
end

#extract_nameObject



155
156
157
# File 'lib/util.rb', line 155

def extract_name
  extract_git_variable("user.name")
end

#generate_ca_installerObject



159
160
161
162
163
164
# File 'lib/util.rb', line 159

def generate_ca_installer
  directory "install_certificate","#{root_dir}/install_certificate"
  FileUtils.cp "#{admin_dir}/ca/rootCA.crt","install_certificate"
  package_tar "install_certificate"
  FileUtils.rm_rf "install_certificate"
end

#install_dirObject



56
57
58
# File 'lib/util.rb', line 56

def install_dir
  "#{admin_dir}/installation_scripts"
end

#package_tar(dir_name, current_dir = nil) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/util.rb', line 166

def package_tar(dir_name,current_dir = nil)
  if current_dir
    `tar -cf #{dir_name}.tar -C #{current_dir} #{dir_name}` 
  else
    `tar -cf #{dir_name}.tar #{dir_name}` 
  end
end

#proxy_dirObject



60
61
62
# File 'lib/util.rb', line 60

def proxy_dir
  "#{root_dir}/proxy"
end

#remove_line_from_routine(routine, filter_line) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/util.rb', line 127

def remove_line_from_routine(routine,filter_line)
  File.open "#{routine_dir}/tmp","w" do | output_file |
    File.foreach "#{routine_dir}/#{routine}" do | line |
    output_file.write line unless line  == "#{filter_line}\n"
  end
  end
  FileUtils.mv "#{routine_dir}/tmp", "#{routine_dir}/#{routine}"
end

#root_dirObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/util.rb', line 9

def root_dir 
  return @root_dir if @root_dir
  search_dir = Dir.pwd
  while search_dir && !root_dir_condition(search_dir)
    parent = File.dirname(search_dir)
    # project_root wird entweder der Root-pfad oder false. Wenn es false
    # wird, bricht die Schleife ab. Vgl. Rails
    search_dir = (parent != search_dir) && parent
  end
  project_root = search_dir if root_dir_condition(search_dir)
  raise 'you are not within a presentation-project.' unless project_root
  @root_dir = Pathname.new(File.realpath project_root)
end

#root_dir_condition(search_dir) ⇒ Object



23
24
25
# File 'lib/util.rb', line 23

def root_dir_condition(search_dir)
  search_dir.is_a?(String) && search_dir.end_with?("/docker") && (Dir.entries(search_dir) && %w{admin backup apps}).length == 3
end

#routine_dirObject



52
53
54
# File 'lib/util.rb', line 52

def routine_dir
  "#{admin_dir}/routines"
end

#runner_dirObject



35
36
37
# File 'lib/util.rb', line 35

def runner_dir
  "#{root_dir}/ci_runner"
end

#service_hooks_for(app_name, type) ⇒ Object



95
96
97
98
# File 'lib/util.rb', line 95

def service_hooks_for(app_name,type)
  Dir.entries("#{apps_dir}/#{app_name}/administration/hooks/#{type}.d")
    .select {| entry | !entry.start_with?(".") && entry != "before_all" && entry != "after_all" }
end

#services(app_name) ⇒ Object



100
101
102
103
104
# File 'lib/util.rb', line 100

def services(app_name)
  YAML.load(File.read("#{apps_dir}/#{app_name}/docker-compose.yml"))
    .each_key
    .select {|k| !k.end_with?("data")}
end

#vhost_dirObject



70
71
72
# File 'lib/util.rb', line 70

def vhost_dir
  "#{proxy_dir}/vhosts.d"
end

#volumes(app_name, service_name) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/util.rb', line 111

def volumes(app_name,service_name)
  raw_volumes = YAML.load(File.read("#{apps_dir}/#{app_name}/docker-compose.yml"))["#{service_name}"]['volumes']
  raw_volumes.map do |volume|
    volume.chop! if volume.end_with? '/'
    volume[0] = '' if volume.start_with? '/'
    volume
  end
end