-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generators #110
Merged
Merged
Generators #110
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f54ce56
feat: add generators to set up any_cable
bibendi db070d3
Update generators verbiage
palkan d88d4bb
chore: remove excess method
bibendi 2362083
feat: add note about action_cable_meta_tag
palkan 8712842
tests: cover setup generator
bibendi f392025
fix: update generatos verbiage, pt 2
palkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
version: '3.4' | ||
version: '2.4' | ||
|
||
services: | ||
app: | ||
image: ruby:2.6 | ||
environment: | ||
- GEM_HOME=/bundle | ||
- BUNDLE_PATH=/bundle | ||
- HISTFILE=/app/tmp/.bash_history | ||
working_dir: /app | ||
volumes: | ||
- ..:/app | ||
- bundle:/bundle | ||
- gems:/usr/local/bundle | ||
|
||
volumes: | ||
bundle: | ||
gems: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
# frozen_string_literal: true | ||
|
||
module AnyCableRailsGenerators | ||
# Entry point for interactive installation | ||
class SetupGenerator < ::Rails::Generators::Base | ||
namespace "anycable:setup" | ||
source_root File.expand_path("templates", __dir__) | ||
|
||
METHODS = %w[skip local docker].freeze | ||
SERVER_VERSION = "v0.6.4" | ||
OS_NAMES = %w[linux darwin freebsd win].freeze | ||
CPU_NAMES = %w[amd64 arm64 386 arm].freeze | ||
SERVER_SOURCES = %w[skip brew binary].freeze | ||
DEFAULT_BIN_PATH = "/usr/local/bin" | ||
|
||
class_option :method, | ||
type: :string, | ||
desc: "Select your development environment to run particular installation method (options: #{METHODS.join(', ')})" | ||
class_option :source, | ||
type: :string, | ||
desc: "Install AnyCable-Go server from particular source (options: #{SERVER_SOURCES.join(', ')})" | ||
class_option :bin_path, | ||
type: :string, | ||
desc: "Install AnyCable-Go server binary to directory (default: #{DEFAULT_BIN_PATH})" | ||
class_option :os, | ||
type: :string, | ||
desc: "Download AnyCable-Go server binary for particular OS (options: #{OS_NAMES.join(', ')})" | ||
class_option :cpu, | ||
type: :string, | ||
desc: "Download AnyCable-Go server binary for particular CPU architecture (options: #{CPU_NAMES.join(', ')})" | ||
class_option :skip_heroku, | ||
type: :boolean, | ||
desc: "Do not copy Heroku configs" | ||
class_option :skip_procfile_dev, | ||
type: :boolean, | ||
desc: "Do not create Procfile.dev" | ||
|
||
def welcome | ||
say "👋 Welcome to AnyCable interactive installer." | ||
end | ||
|
||
def configs | ||
inside("config") do | ||
template "cable.yml" | ||
template "anycable.yml" | ||
end | ||
end | ||
|
||
def cable_url | ||
environment(nil, env: :development) do | ||
<<~SNIPPET | ||
# Specify AnyCable WebSocket server URL to use by JS client | ||
config.action_cable.url = ENV.fetch("CABLE_URL", "ws://localhost:3334/cable").presence | ||
SNIPPET | ||
end | ||
|
||
environment(nil, env: :production) do | ||
<<~SNIPPET | ||
# Specify AnyCable WebSocket server URL to use by JS client | ||
config.action_cable.url = ENV["CABLE_URL"].presence | ||
SNIPPET | ||
end | ||
|
||
say_status :info, "✅ 'config.action_cable.url' has been configured" | ||
say_status :help, "️️⚠️ Make sure you have `action_cable_meta_tag` in your application.html if you're using JS client" | ||
end | ||
|
||
def development_method | ||
answer = METHODS.index(options[:method]) || 99 | ||
|
||
until METHODS[answer.to_i] | ||
answer = ask "Which environment do you use for development? (1) Local, (2) Docker, (0) Skip" | ||
end | ||
|
||
case env = METHODS[answer.to_i] | ||
when "skip" | ||
say_status :help, "⚠️ Please, read this guide on how to install AnyCable-Go server 👉 https://docs.anycable.io/#/anycable-go/getting_started", :yellow | ||
else | ||
send "install_for_#{env}" | ||
end | ||
end | ||
|
||
def heroku | ||
if options[:skip_heroku].nil? | ||
return unless yes? "Do you use Heroku for deployment?" | ||
elsif options[:skip_heroku] | ||
return | ||
end | ||
|
||
template "Procfile" | ||
inside("bin") { template "heroku-web" } | ||
|
||
say_status :help, "️️⚠️ Please, read the required steps to configure Heroku applications 👉 https://docs.anycable.io/#/deployment/heroku", :yellow | ||
end | ||
|
||
def finish | ||
say_status :info, "✅ AnyCable has been configured successfully!" | ||
end | ||
|
||
private | ||
|
||
def install_for_docker | ||
say_status :help, "️️⚠️ Docker development configuration could vary", :yellow | ||
|
||
say "Here is an example snippet for docker-compose.yml:" | ||
say <<~YML | ||
───────────────────────────────────────── | ||
anycable-ws: | ||
image: anycable/anycable-go:v0.6.4 | ||
ports: | ||
- '3334:3334' | ||
environment: | ||
PORT: 3334 | ||
REDIS_URL: redis://redis:6379/0 | ||
ANYCABLE_RPC_HOST: anycable-rpc:50051 | ||
depends_on: | ||
- anycable-rpc | ||
- redis | ||
|
||
anycable-rpc: | ||
<<: *backend | ||
command: bundle exec anycable | ||
ports: | ||
- '50051' | ||
───────────────────────────────────────── | ||
YML | ||
end | ||
|
||
def install_for_local | ||
install_server | ||
template_proc_files | ||
end | ||
|
||
def install_server | ||
answer = SERVER_SOURCES.index(options[:source]) || 99 | ||
|
||
until SERVER_SOURCES[answer.to_i] | ||
answer = ask "How do you want to install AnyCable-Go WebSocket server? (1) Homebrew, (2) Download binary, (0) Skip" | ||
end | ||
|
||
case answer.to_i | ||
when 0 | ||
say_status :help, "⚠️ Please, read this guide on how to install AnyCable-Go server 👉 https://docs.anycable.io/#/anycable-go/getting_started", :yellow | ||
return | ||
else | ||
return unless send("install_from_#{SERVER_SOURCES[answer.to_i]}") | ||
end | ||
|
||
say_status :info, "✅ AnyCable-Go WebSocket server has been successfully installed" | ||
end | ||
|
||
def template_proc_files | ||
file_name = "Procfile.dev" | ||
|
||
if File.exist?(file_name) | ||
append_file file_name, 'anycable: bundle exec anycable --server-command "anycable-go --port 3334"' | ||
else | ||
say_status :help, "💡 We recommend using Hivemind to manage multiple processes in development 👉 https://github.com/DarthSim/hivemind", :yellow | ||
|
||
if options[:skip_procfile_dev].nil? | ||
return unless yes? "Do you want to create a '#{file_name}' file?" | ||
elsif options[:skip_procfile_dev] | ||
return | ||
end | ||
|
||
template file_name | ||
end | ||
end | ||
|
||
def install_from_brew | ||
run "brew install anycable-go", abort_on_failure: true | ||
run "anycable-go -v", abort_on_failure: true | ||
end | ||
|
||
def install_from_binary | ||
out = options[:bin_path] if options[:bin_path] | ||
out ||= "/usr/local/bin" if options[:method] # User don't want interactive mode | ||
out ||= ask "Please, enter the path to download the AnyCable-Go binary to", default: DEFAULT_BIN_PATH, path: true | ||
|
||
os_name = OS_NAMES.find(&Gem::Platform.local.os.method(:==)) || | ||
options[:os] || | ||
ask("What is your OS name?", limited_to: OS_NAMES) | ||
|
||
cpu_name = CPU_NAMES.find(¤t_cpu.method(:==)) || | ||
options[:cpu] || | ||
ask("What is your CPU architecture?", limited_to: CPU_NAMES) | ||
|
||
download_exe( | ||
"https://github.com/anycable/anycable-go/releases/download/#{SERVER_VERSION}/" \ | ||
"anycable-go-#{SERVER_VERSION}-#{os_name}-#{cpu_name}", | ||
to: out, | ||
file_name: "anycable-go" | ||
) | ||
|
||
true | ||
end | ||
|
||
def download_exe(url, to:, file_name:) | ||
file_path = File.join(to, file_name) | ||
|
||
run "#{sudo(to)}curl -L #{url} -o #{file_path}", abort_on_failure: true | ||
run "#{sudo(to)}chmod +x #{file_path}", abort_on_failure: true | ||
run "#{file_path} -v", abort_on_failure: true | ||
end | ||
|
||
def sudo!(path) | ||
sudo = "" | ||
unless File.writable?(path) | ||
if yes? "Path is not writable 😕. Do you have sudo privileges?" | ||
sudo = "sudo " | ||
else | ||
say_status :error, "❌ Failed to install AnyCable-Go WebSocket server", :red | ||
raise StandardError, "Path #{path} is not writable!" | ||
end | ||
end | ||
|
||
sudo | ||
end | ||
|
||
def current_cpu | ||
case Gem::Platform.local.cpu | ||
when "x86_64", "x64" | ||
"amd64" | ||
when "x86_32", "x86", "i386", "i486", "i686" | ||
"i386" | ||
when "aarch64", "aarch64_be", /armv8/ | ||
"arm64" | ||
when "arm", /armv7/, /armv6/ | ||
"arm" | ||
else | ||
"unknown" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
web: bin/heroku-web | ||
release: bundle exec rails db:migrate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
server: bin/rails server | ||
assets: bin/webpack-dev-server | ||
anycable: bundle exec anycable --server-command "anycable-go --port 3334" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
if [ "$ANYCABLE_DEPLOYMENT" == "true" ]; then | ||
bundle exec anycable --server-command="anycable-go" | ||
else | ||
bundle exec rails server -p $PORT -b 0.0.0.0 | ||
fi |
30 changes: 30 additions & 0 deletions
30
lib/rails/generators/anycable/setup/templates/config/anycable.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file contains per-environment settings for AnyCable. | ||
# | ||
# Since AnyCable config is based on anyway_config (https://github.com/palkan/anyway_config), all AnyCable settings | ||
# can be set or overridden through the corresponding environment variables. | ||
# E.g., `rpc_host` is overridden by ANYCABLE_RPC_HOST, `debug` by ANYCABLE_DEBUG etc. | ||
# | ||
# Note that AnyCable recognizes REDIS_URL env variable for Redis pub/sub adapter. If you want to | ||
# use another Redis instance for AnyCable, provide ANYCABLE_REDIS_URL variable. | ||
# | ||
# Read more about AnyCable configuration here: https://docs.anycable.io/#/ruby/configuration | ||
# | ||
default: &default | ||
# Turn on/off access logs ("Started..." and "Finished...") | ||
access_logs_disabled: false | ||
# This is the host and the port to run AnyCable RPC server on. | ||
# You must configure your WebSocket server to connect to it, e.g.: | ||
# $ anycable-go --rpc-host="<rpc hostname>:50051" | ||
rpc_host: "127.0.0.1:50051" | ||
# Whether to enable gRPC level logging or not | ||
log_grpc: false | ||
# Use the same channel name for WebSocket server, e.g.: | ||
# $ anycable-go --redis-channel="__anycable__" | ||
redis_channel: "__anycable__" | ||
|
||
development: | ||
<<: *default | ||
redis_url: "redis://localhost:6379/1" | ||
|
||
production: | ||
<<: *default |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We definitely need to not use a server version in a constant. Otherwise, we will forget to change that constant every time after the server was released.
There is a secret url that can get the latest asset from the latest release.
https://github.com/anycable/anycable-go/releases/latest/download/anycable-go-v0.6.4-linux-amd64
The big problem we have is that a binary contains a version in a name. This is not the necessary information. I propose to cut it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@palkan What do think? Can we roll out a new release with new naming to have the ability to change the version to the
latest
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's a good idea. Let's add a ticket to the v1.0 project.
For now, let's leave as is (let's add a
TODO:
orFIXME:
with this comment), so we'll be able to test the generator without waiting for anycable-go fixes.