Class: Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/chagall/install/main.rb

Overview

The Installer class is responsible for setting up the environment for a Ruby on Rails application. It detects the required services and versions, generates necessary Docker and Compose files, and logs the process.

Constants:

  • TEMPLATES_DIR: Directory where template files are stored.

  • DEFAULT_NODE_VERSION: Default Node.js version to use if not specified.

  • DEFAULT_RUBY_VERSION: Default Ruby version to use if not specified.

Attributes:

  • app_name: Name of the application.

  • services: List of services to be included in the setup.

  • versions: Hash containing versions of Ruby, Node.js, PostgreSQL, and Redis.

  • logger: Logger instance for logging messages.

  • database_type: Type of database to use (e.g., ‘postgres’, ‘mysql’, ‘sqlite’).

Methods:

  • initialize(options = {}): Initializes the installer with given options.

  • install: Main method to perform the installation process.

Private Methods:

  • backup_file(file): Creates a backup of the specified file if it exists.

  • detect_ruby_version: Detects the Ruby version from various files or defaults to a predefined version.

  • detect_node_version: Detects the Node.js version from various files or defaults to a predefined version.

  • node_version_from_package_json: Extracts the Node.js version from package.json if available.

  • node_version_from_file: Extracts the Node.js version from .node-version, .tool-versions, or .nvmrc files if available.

  • detect_services: Detects required services based on the gems listed in the Gemfile.

  • generate_compose: Generates the Docker Compose file from a template.

  • generate_dockerfile: Generates the Dockerfile from a template.

Constant Summary collapse

TEMPLATES_DIR =

rubocop:disable Metrics/ClassLength

File.expand_path("./templates", __dir__)
TEMP_DIR =
File.join(Dir.tmpdir, "chagall-#{SecureRandom.hex(4)}").freeze
DEFAULT_RUBY_VERSION =
"3.3.0"
DEFAULT_NODE_VERSION =
"20.11.0"
GITHUB_REPO =
"frontandstart/chagall"
TEMPLATE_FILES =
%w[template.compose.yaml
template.Dockerfile].freeze
DEPENDENCIES =
[
  {
    adapter: :postgresql,
    gem_name: "pg",
    service: :postgres,
    image: "postgres:16.4-bullseye",
    docker_env: "DATABASE_URL: postgres://postgres:postgres@postgres:5432"
  },
  {
    adapter: :mysql2,
    gem_name: "mysql2",
    service: :mariadb,
    image: "mariadb:8.0-bullseye",
    docker_env: "DATABASE_URL: mysql://mysql:mysql@mariadb:3306"
  },
  {
    adapter: :mongoid,
    gem_name: "mongoid",
    service: :mongodb,
    image: "mongo:8.0-noble",
    docker_env: "DATABASE_URL: mongodb://mongodb:27017"
  },
  {
    gem_name: "redis",
    service: :redis,
    image: "redis:7.4-bookworm",
    docker_env: "REDIS_URL: redis://redis:6379"
  },
  {
    gem_name: "sidekiq",
    service: :sidekiq,
    image: -> { app_name }
  },
  {
    gem_name: "elasticsearch",
    service: :elasticsearch,
    image: "elasticsearch:8.15.3",
    docker_env: "ELASTICSEARCH_URL: elasticsearch://elasticsearch:9200"
  },
  {
    gem_name: "solid_queue",
    service: :solid_queue,
    image: -> { app_name }
  }
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Installer

Returns a new instance of Installer.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chagall/install/main.rb', line 110

def initialize(options = {})
  raise "Gemfile not found" unless File.exist?("Gemfile")

  Chagall::Settings.configure(argv)

  @app_name = options[:app_name] || File.basename(Dir.pwd)
  @services = []
  @logger = Logger.new($stdout)
  @logger.formatter = proc { |_, _, _, msg| "#{msg}\n" }
  @environments = {}
  @gemfile = File.read("Gemfile")
  @gemfile_lock = File.read("Gemfile.lock")
  @database_adapters = YAML.load_file("config/database.yml")
                           .map { |_, config| config["adapter"] }.uniq
  @database_type = @database_adapters
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



99
100
101
# File 'lib/chagall/install/main.rb', line 99

def app_name
  @app_name
end

#database_configObject (readonly)

Returns the value of attribute database_config.



99
100
101
# File 'lib/chagall/install/main.rb', line 99

def database_config
  @database_config
end

#database_typeObject (readonly)

Returns the value of attribute database_type.



99
100
101
# File 'lib/chagall/install/main.rb', line 99

def database_type
  @database_type
end

#environmentsObject

Returns the value of attribute environments.



107
108
109
# File 'lib/chagall/install/main.rb', line 107

def environments
  @environments
end

#gemfileObject (readonly)

Returns the value of attribute gemfile.



99
100
101
# File 'lib/chagall/install/main.rb', line 99

def gemfile
  @gemfile
end

#gemfile_lockObject (readonly)

Returns the value of attribute gemfile_lock.



99
100
101
# File 'lib/chagall/install/main.rb', line 99

def gemfile_lock
  @gemfile_lock
end

#loggerObject (readonly)

Returns the value of attribute logger.



99
100
101
# File 'lib/chagall/install/main.rb', line 99

def logger
  @logger
end

#project_servicesObject

Returns the value of attribute project_services.



107
108
109
# File 'lib/chagall/install/main.rb', line 107

def project_services
  @project_services
end

#versionsObject (readonly)

Returns the value of attribute versions.



99
100
101
# File 'lib/chagall/install/main.rb', line 99

def versions
  @versions
end

Instance Method Details

#installObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/chagall/install/main.rb', line 127

def install
  setup_temp_directory
  detect_services
  generate_environment_variables
  generate_compose_file
  generate_dockerfile
  logger.info "Installation completed successfully!"
ensure
  cleanup_temp_directory
end