Class: HandsomeFencer::CircleCI::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/handsome_fencer/circle_c_i/cli.rb,
lib/handsome_fencer/circle_c_i/cli/expose.rb,
lib/handsome_fencer/circle_c_i/cli/dockerize.rb,
lib/handsome_fencer/circle_c_i/cli/obfuscate.rb,
lib/handsome_fencer/circle_c_i/cli/generate_key.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



13
14
15
# File 'lib/handsome_fencer/circle_c_i/cli.rb', line 13

def self.source_root
  File.dirname(__FILE__) + '/templates/'
end

Instance Method Details

#dockerizeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/handsome_fencer/circle_c_i/cli/dockerize.rb', line 10

def dockerize

  directory "circleci", "./.circleci", recursive: true
  directory "docker", "docker", recursive: true
  directory "lib", "lib", recursive: true
  copy_file "docker-compose.yml", "docker-compose.yml"
  copy_file "Gemfile", "Gemfile"
  copy_file "Gemfile.lock", "Gemfile.lock"
  copy_file "config/database.yml", "config/database.yml"
  copy_file "gitignore", ".gitignore"
  append_to_file ".gitignore", "\ndocker/**/*.env"
  append_to_file ".gitignore", "\ndocker/**/*.key"

  default_values = {
    "APP_NAME" => "greenfield",
    "SERVER_HOST" => "ip-address-of-your-server",
    "DOCKERHUB_EMAIL" => "your-docker-hub-emaill",
    "DOCKERHUB_USER" => "your-docker-hub-user-name",
    "DOCKERHUB_ORG" => "your-docker-hub-org-name",
    "DOCKERHUB_PASS" => "your-docker-hub-password",
    "POSTGRES_USER" => "your-postgres-username",
    "POSTGRES_PASSWORD" => "your-postgres-password"
  }

  prompts = {
    "APP_NAME" => "the name of your app",
    "SERVER_HOST" => "the ip address of your server",
    "DOCKERHUB_EMAIL" => "your Docker Hub email",
    "DOCKERHUB_USER" => "your Docker Hub username",
    "DOCKERHUB_ORG" => "your Docker Hub organization name",
    "DOCKERHUB_PASS" => "your Docker Hub password",
    "POSTGRES_USER" => "your Postgres username",
    "POSTGRES_PASSWORD" => "your Postgres password"
  }

  prompts.map do |key, prompt|
    prompts[key] = ask("Please provide #{prompt}:")
    if prompts[key].size == 0
      prompts[key] = default_values[key]
    end
  end

  # account_type = ask("Will you be pushing images to Docker Hub under your user name or under your organization name instead?", :limited_to => ["org", "user", "skip"])
  # case account_type
  #
  # when "o"
  #   prompts['DOCKERHUB_ORG_NAME']= ask("Organization name:")
  # when "u"
  #   prompts['DOCKERHUB_ORG_NAME']= "${DOCKERHUB_USER}"
  # when "s"
  #   prompts['DOCKERHUB_ORG_NAME']= "${DOCKERHUB_USER}"
  # end
  prompts.map do |key, value|
    append_to_file 'docker/env_files/circleci.env', "\nexport #{key}=#{value}"
  end

  %w[development circleci staging production].each do |environment|
    base = "docker/containers/"

    app_env = create_file "#{base}app/#{environment}.env"
    append_to_file app_env, "DATABASE_HOST=database\n"
    append_to_file app_env, "RAILS_ENV=#{environment}\n"

    database_env = create_file "#{base}database/#{environment}.env"
    append_to_file database_env, "POSTGRES_USER=postgres\n"
    append_to_file database_env, "POSTGRES_DB=#{prompts['APP_NAME']}_#{environment}\n"
    append_to_file database_env, "POSTGRES_PASSWORD=#{prompts['POSTGRES_PASSWORD']}\n"

    ssl = (environment == "production") ? true : false
    web_env = create_file "#{base}web/#{environment}.env"
    append_to_file web_env, "CA_SSL=postgres#{ssl}\n"
  end
  %w[circleci production].each do |environment|
    template "docker/overrides/#{environment}.yml.tt", "docker/overrides/#{environment}.yml"
  end

  %w[app web].each do |container|
    options = {
      email: prompts['DOCKERHUB_EMAIL'],
      app_name: prompts['APP_NAME']
    }
    template "docker/containers/#{container}/Dockerfile.tt", "docker/containers/#{container}/Dockerfile", options
  end
end

#expose(*args) ⇒ Object



7
8
9
10
11
# File 'lib/handsome_fencer/circle_c_i/cli/expose.rb', line 7

def expose(*args)
  environment = args.first
  @cipher = HandsomeFencer::CircleCI::Crypto.new(environment: environment)
  @cipher.expose('docker', "#{environment}.env.enc")
end

#generate_key(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/handsome_fencer/circle_c_i/cli/generate_key.rb', line 7

def generate_key(*args)
  default_environments = %w[circleci development production]
  environments = args.first ? [args.first] : default_environments
  environments.each do |environment|
    @cipher = OpenSSL::Cipher.new 'AES-128-CBC'
    @salt = '8 octets'
    @new_key = @cipher.random_key

    create_file "docker/keys/#{environment}.key", Base64.encode64(@new_key)
  end
end

#obfuscate(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/handsome_fencer/circle_c_i/cli/obfuscate.rb', line 8

def obfuscate(*args)

  default_environments = %w[circleci development staging production]
  environments = args.first ? args.first : default_environments
  environments.each do |environment|
    @cipher = HandsomeFencer::CircleCI::Crypto.new(environment: environment)
    @cipher.obfuscate('docker', "#{environment}.env")
  end
end