Class: Mobilis::Project

Inherits:
Object
  • Object
show all
Includes:
ActionsProjectsTake, NewRelic
Defined in:
lib/mobilis/project.rb

Overview

this is NOT a GenericProject, this is the metaproject TODO: s/Project/Meta/g

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NewRelic

#enable_distributed_tracing, #new_relic, #set_license_key

Methods included from ActionsProjectsTake

#append_line, #oblivious_run_command, #run_command, #run_docker, #set_file_contents, #set_second_line, #write_file

Constructor Details

#initializeProject

Returns a new instance of Project.



27
28
29
30
31
32
33
34
# File 'lib/mobilis/project.rb', line 27

def initialize
  @data = {
    projects: [],
    username: ENV.fetch('USER', ENV.fetch('USERNAME', '')),
    starting_port_no: 10000,
    port_gap: 100
    }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/mobilis/project.rb', line 25

def name
  @name
end

Instance Method Details

#add_mysql_instance(name) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/mobilis/project.rb', line 215

def add_mysql_instance name
  data = {
    name: name,
    type: :mysql
  }
  @data[:projects] << data
  GenericProject.new data, self
end

#add_omakase_stack_rails_project(name) ⇒ Object



202
203
204
# File 'lib/mobilis/project.rb', line 202

def add_omakase_stack_rails_project name
  add_rails_project name, [:simplecov, :standard, :api]
end

#add_postgresql_instance(name) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/mobilis/project.rb', line 206

def add_postgresql_instance name
  data = {
    name: name,
    type: :postgresql
  }
  @data[:projects] << data
  GenericProject.new data, self
end

#add_prime_stack_rails_project(name) ⇒ Object



198
199
200
# File 'lib/mobilis/project.rb', line 198

def add_prime_stack_rails_project name
  add_rails_project name, [:rspec, :api, :simplecov, :standard, :factorybot]
end

#add_rack_project(name) ⇒ Object



246
247
248
249
250
251
252
253
254
# File 'lib/mobilis/project.rb', line 246

def add_rack_project name
  data = {
    name: name,
    type: :rack,
    attributes: {}
  }
  @data[:projects] << data
  RackProject.new data, self
end

#add_rails_project(name, options) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/mobilis/project.rb', line 233

def add_rails_project name, options
  data = {
    name: name,
    type: :rails,
    controllers: [],
    models: [],
    options: options.clone,
    attributes: {}
  }
  @data[:projects] << data
  RailsProject.new data, self
end

#add_redis_instance(name) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/mobilis/project.rb', line 224

def add_redis_instance name
  data = {
    name: name,
    type: :redis,
  }
  @data[:projects] << data
  RedisInstance.new data, self
end

#attributesObject



36
37
38
# File 'lib/mobilis/project.rb', line 36

def attributes
  generate_attributes
end

#buildObject



145
146
147
148
149
150
# File 'lib/mobilis/project.rb', line 145

def build
#  logger.info "# build"
  Dir.chdir 'generate' do
    run_command "docker compose build"
  end
end

#build_rails_builderObject



156
157
158
# File 'lib/mobilis/project.rb', line 156

def build_rails_builder
  run_docker "build -t #{ rails_builder_image } ."
end

#create_rails_builderObject



62
63
64
65
66
67
68
69
# File 'lib/mobilis/project.rb', line 62

def create_rails_builder
  Dir.mkdir 'rails-builder'
  Dir.chdir 'rails-builder' do
    create_rails_builder_dockerfile
    create_rails_builder_gemfile
    build_rails_builder
  end
end

#create_rails_builder_dockerfileObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mobilis/project.rb', line 71

def create_rails_builder_dockerfile
  set_file_contents 'Dockerfile', <<EOF
FROM ruby:latest
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
# Common dependencies
RUN apt-get update -qq \\
  && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \\
    build-essential \\
    gnupg2 \\
    curl \\
    less \\
    git \\
    nodejs \\
    postgresql-client \\
  && apt-get clean \\
  && rm -rf /var/cache/apt/archives/* \\
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \\
  && truncate -s 0 /var/log/*log
RUN gem update bundle
RUN gem update --system
COPY Gemfile .
RUN bundle install
EOF
end

#create_rails_builder_gemfileObject



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
# File 'lib/mobilis/project.rb', line 96

def create_rails_builder_gemfile
  set_file_contents 'Gemfile', <<EOF
source "https://rubygems.org"
# FIXME
#git_source(:github) { |repo| "https://github.com/repo.git" }

gem "rails"
gem "sqlite3"
gem "puma"
gem "jbuilder"
gem "redis"
gem "kredis"
gem "bcrypt"
gem "bootsnap", require: false
gem "image_processing"
gem "rack-cors"
gem "pg"
gem "mysql2"
gem "minitest"

group :development, :test do
  gem "debug", platforms: %i[ mri mingw x64_mingw ]
end

group :development do
  gem "spring"
end

gem "rspec-rails", group: [:development, :test]
EOF

end

#displayObject



194
195
196
# File 'lib/mobilis/project.rb', line 194

def display
  ap @data
end

#generate_attributesObject



136
137
138
139
140
141
142
143
# File 'lib/mobilis/project.rb', line 136

def generate_attributes
  attributes = {projects: {}, new_relic_license_key: ENV.fetch("NEW_RELIC_LICENSE_KEY", "some_invalid_key_NREAL")}
  projects.each_with_index do |project, index|
    attributes["#{project.name}_internal_port_no".to_sym] =
      @data[:starting_port_no] + (index * @data[:port_gap])
  end
  attributes
end

#generate_filesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mobilis/project.rb', line 44

def generate_files
  if Dir.exist? 'generate'
    puts "Removing existing generate directory"
    FileUtils.rm_rf('generate')
  end

  Dir.mkdir 'generate'

  Dir.chdir 'generate' do
    save_project
    create_rails_builder if has_rails_project?
    projects.each_with_index do |project, index|
      project.generate
    end
    save_docker_compose
  end
end

#getwdObject



256
257
258
259
260
# File 'lib/mobilis/project.rb', line 256

def getwd
  wd = Dir.getwd
  return wd unless wd[1] == ":"
  return "/#{wd[0]}#{wd[2...]}"
end

#has_rails_project?Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
# File 'lib/mobilis/project.rb', line 129

def has_rails_project?
  projects.each do |p|
    return true if p.type.to_sym == :rails
  end
  return false
end

#load_from_file(filename) ⇒ Object



160
161
162
163
# File 'lib/mobilis/project.rb', line 160

def load_from_file filename
  data = File.read filename
  @data = JSON.parse data, {:symbolize_names => true}
end

#project_by_name(name) ⇒ Object



190
191
192
# File 'lib/mobilis/project.rb', line 190

def project_by_name name
  projects.find {|p| p.name == name }
end

#projectsObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/mobilis/project.rb', line 179

def projects
  mapping = {
    rails: RailsProject,
    postgresql: PostgresqlInstance,
    mysql: MysqlInstance,
    redis: RedisInstance,
    rack: RackProject
  }
  @data[:projects].map {|p| mapping[p[:type].to_sym].new p, self}
end

#rails_builder_imageObject



152
153
154
# File 'lib/mobilis/project.rb', line 152

def rails_builder_image
  "#{ username }/rails-builder"
end

#save_docker_composeObject



171
172
173
174
175
176
177
# File 'lib/mobilis/project.rb', line 171

def save_docker_compose
  docker = DockerComposeProjector.project self

  File.open("docker-compose.yml", "w") do |f|
    f.write(docker.to_yaml)
  end
end

#save_projectObject



165
166
167
168
169
# File 'lib/mobilis/project.rb', line 165

def save_project
  File.open("mproj.json", "w") do |f|
    f.write(JSON.pretty_generate(@data))
  end
end

#usernameObject



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

def username
  @data[:username]
end