Class: InitializeProject

Inherits:
Object
  • Object
show all
Defined in:
lib/support/initialize_project.rb

Instance Method Summary collapse

Instance Method Details

#app_docker_pathObject



4
5
6
7
8
9
# File 'lib/support/initialize_project.rb', line 4

def app_docker_path
  project_dir = FileUtils.pwd
  docker_dir = "#{project_dir}/docker"
  FileUtils.mkdir_p("#{docker_dir}/files")
  return docker_dir
end

#create_configurationObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
129
# File 'lib/support/initialize_project.rb', line 26

def create_configuration
  #Use directory name as default for image name.
  default_image_name = FileUtils.pwd.split('/').last
  default_image_name = default_image_name.split('_').join("-")
  default_image_name = default_image_name.split(' ').join("-")
  
  puts "Creating configurations."
  puts "Enter image name of rails app (#{default_image_name}):"
  image_name = STDIN.gets.chomp
  if (image_name.length == 0)
    image_name = default_image_name
  end
  
  db_run_time_name = image_name + "-db"
  
  docker_dir = app_docker_path()
  FileUtils.mkdir_p("#{docker_dir}/files")
  
  this_dir = File.dirname(__FILE__)
  docker_file_path = "#{docker_dir}/Dockerfile"
  copy_docker_file = true
  if (File.exist?(docker_file_path))
    puts "Dockerfile already exists in #{docker_dir}.  Overwrite? (y/n)"
    overwrite = STDIN.gets.chomp
    if (overwrite.downcase == "n")
      copy_docker_file = false
    end
  end
  
  if (copy_docker_file)
    FileUtils.cp("#{this_dir}/image_files/DockerfileSelf", docker_file_path)
    fill_in_file_param(docker_file_path, 'SQL_HOST', db_run_time_name)
    puts "Copied standard Dockerfile to: #{docker_dir}"
  end
  
  
  copy_entry_file = true
  entry_file_path = "#{docker_dir}/entrypoint.sh"
  if (File.exist?(entry_file_path))
    puts "Entrypoint.sh already exists in #{docker_dir}.  Overwrite? (y/n)"
    overwrite = STDIN.gets.chomp
    if (overwrite.downcase == "n")
      copy_entry_file = false
    end
  end
  
  if (copy_entry_file)
    FileUtils.cp("#{this_dir}/image_files/entrypoint.sh", "#{docker_dir}/entrypoint.sh")
    puts "Copied entry point file to: #{docker_dir}"
  end

  FileUtils.cp("#{this_dir}/image_files/wait_for_port.sh", "#{docker_dir}/wait_for_port.sh")
  
  
  main_configuration = {
    registry: "https://my.registry.com:5000",
    build: {
      name: image_name
    },
    run: {
      name: image_name,
      options: {
        "Env" => [
          "IS_DOCKER=true"
        ],
        "HostConfig" => {
          "Links" => ["#{db_run_time_name}:#{db_run_time_name}"],
          "PortBindings" => {
            "3000/tcp" => [
                    {"HostIp" => "", "HostPort" => "3000"}
            ]
          }
        }
      }
    }
  }
  
  sql_configuration = {
    run: {
      name: db_run_time_name,
      image_name: "percona:5.6",
      options: {
        "Env" => [
          "MYSQL_ROOT_PASSWORD=password"
        ],
        "HostConfig" => {
          "PortBindings" => {
            "3306/tcp" => []
          }
        }
      }
    }
  }
  
  
  configs = [{sql: sql_configuration}, {rails_app: main_configuration}]
  
  output = JSON.pretty_generate(configs)
  config_file_path = "#{app_docker_path}/docker-rails.json"
  File.open(config_file_path, "w") do |f|
    f.write(output)
  end
  
end

#fill_in_file_param(file_path, param_name, param_value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/support/initialize_project.rb', line 11

def fill_in_file_param(file_path, param_name, param_value)
  new_content = nil
  File.open(file_path, "r") do |file|
    content = file.read
    search_str = "<<#{param_name}>>"
    index = content.index(search_str)
    content_start = content[0..(index - 1)]
    content_end = content[(index + search_str.length), content.length]
    new_content = content_start + param_value + content_end
  end
  File.open(file_path, "w") do |file|
    file.write(new_content)
  end
end