Skip to content
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

fly.io deployment #950

Open
gedw99 opened this issue Dec 24, 2023 · 0 comments
Open

fly.io deployment #950

gedw99 opened this issue Dec 24, 2023 · 0 comments
Assignees
Labels
feature request New feature or request

Comments

@gedw99
Copy link

gedw99 commented Dec 24, 2023

Fly has a semi managed Postresql, and so just needs the docker file like ghcr.io/permify/permify:latest

https://fly.io/docs/hands-on/sign-up/

https://fly.io/docs/hands-on/install-flyctl

Fly.io Postresql: https://fly.io/docs/postgres/getting-started/what-you-should-know/

This is a fly yaml template:

app = "{{getenv "FLY_APP_NAME"}}" # Replace FLY_APP_NAME with the name of your app
kill_signal = "SIGINT"
kill_timeout = 5
primary_region = "{{getenv "FLY_APP_REGION"}}"  # # Replace FLY_APP_REGION with the region. See a list of regions here: https://fly.io/docs/reference/regions/

[experimental]
    auto_rollback = true
    private_network = true  # Allows server to connect to the database

[build]
     image = "ghcr.io/permify/permify:latest"

[env]
    CODER_ACCESS_URL = "https://{{getenv "FLY_APP_NAME"}}.fly.dev" # Replace FLY_APP_NAME with the name of your app
    CODER_HTTP_ADDRESS = "0.0.0.0:3000"
    #CODER_VERBOSE = "true" # Uncomment this if you want to see more logs
    CODER_TELEMETRY_INSTALL_SOURCE = "fly.io"

[[services]]
    protocol = "tcp"
    internal_port = 3000
    processes = ["app"]

    [[services.ports]]
        port = 80
        handlers = ["http"]
        force_https = true

    [[services.ports]]
        port = 443
        handlers = ["tls", "http"]
    [services.concurrency]
        type = "connections"
        hard_limit = 25

This is a Makefile that runs it:

OS_NAME=$(shell uname)
# Github CI detection. See: https://docs.github.com/en/actions/learn-github-actions/variables
CI_GITHUB=$(CI)

# flyctl binary
FLYCTL_BIN_NAME=flyctl
ifeq ($(shell uname),Windows)
	FLYCTL_BIN_NAME=flyctl.exe
endif
FLYCTL_BIN_WHICH=$(shell which $(FLYCTL_BIN_NAME))
FLYCTL_BIN_WHICH_VERSION=$(shell $(FLYCTL_BIN_NAME) version)

## Variables for fly deployment are held in .env files, so that the Makefile is generic.
# devs variables
# NOTE: This can be .gitignored or not, depending on how you like to manage secrets, etc.
-include .env
## ci variables
# For debugging; toggle this to pretend your inside github ci, so that you can locally check things work .
#CI_GITHUB=TRUE
ifeq ($(CI_GITHUB),TRUE)
-include .env-ci
endif

export FLY_APP_NAME:=$(FLY_APP_NAME)
export FLY_APP_NAME_DB:=$(FLY_APP_NAME_DB)
export FLY_APP_REGION:=$(FLY_APP_REGION)
export PG_CONNECTION_URL:=$(PG_CONNECTION_URL)

print:
	@echo ""
	@echo "- os"
	@echo "OS_NAME:                     $(OS_NAME)"
	@echo "CI_GITHUB:                   $(CI_GITHUB)"
	
	@echo ""
	@echo "- flyctl tool"
	@echo "FLYCTL_BIN_NAME:             $(FLYCTL_BIN_NAME)"
	@echo "FLYCTL_BIN_WHICH:            $(FLYCTL_BIN_WHICH)"
	@echo "FLYCTL_BIN_WHICH_VERSION:    $(FLYCTL_BIN_WHICH_VERSION)"
	@echo ""

	$(MAKE) print-env

print-env:
	@echo ""
	@echo "- variables used by fly"
	@echo "FLY_APP_NAME:                $(FLY_APP_NAME)"
	@echo "FLY_APP_NAME_DB:             $(FLY_APP_NAME_DB)"
	@echo "FLY_APP_REGION:              $(FLY_APP_REGION)"
	@echo "PG_CONNECTION_URL:           $(PG_CONNECTION_URL)"
	@echo ""

ci-run:
	# This is called by github CI. currently just a smoke test deployment.

	$(MAKE) print
	$(MAKE) 0-deploy-setup
	$(MAKE) print
	$(MAKE) 0-deploy-setup-del
	$(MAKE) print
	$(MAKE) 10-deploy-destroy-all

0-deploy-setup:
	# install the flyctl
	# see: https://fly.io/docs/hands-on/install-flyctl

ifeq ($(shell uname),Windows)
	pwsh -Command "iwr https://fly.io/install.ps1 -useb | iex"
endif
ifeq ($(shell uname),Darwin)
	brew install flyctl
endif
ifeq ($(shell uname),Linux)
	brew install flyctl
endif

0-deploy-setup-del: 
	# deletes flyctl.

ifeq ($(shell uname),Windows)
	# Not sure as i dont use windows ..
endif
ifeq ($(shell uname),Darwin)
	brew uninstall flyctl
endif
ifeq ($(shell uname),Linux)
	brew uninstall flyctl
endif


1-deploy-auth:
	# signup or login. Its manages both...
	$(FLYCTL_BIN_NAME) auth signup
2-deploy-list:
	# list what you already have deployed.
	$(FLYCTL_BIN_NAME) apps list
	$(FLYCTL_BIN_NAME) postgres list
3-deploy-create:
	$(FLYCTL_BIN_NAME) postgres create --name $(FLY_APP_NAME_DB) --region $(FLY_APP_REGION)
	$(FLYCTL_BIN_NAME) apps create --name $(FLY_APP_NAME) --region $(FLY_APP_REGION)
	$(FLYCTL_BIN_NAME) postgres attach --app $(FLY_APP_NAME) $(FLY_APP_NAME_DB)
	# copy PG_CONNECTION_URL to Makefile...
4-deploy-secrets:
	$(FLYCTL_BIN_NAME) secrets set CODER_PG_CONNECTION_URL=$(PG_CONNECTION_URL) --app $(FLY_APP_NAME)
5-deploy-run: 
	# Create a fly.toml from the env variables.
	# If your starting new then copy the .env-template to .env and change the variables

	# https://github.com/hairyhenderson/gomplate
	go install github.com/hairyhenderson/gomplate/v4/cmd/gomplate@latest

	gomplate -f fly-template.toml -o fly.toml

	$(FLYCTL_BIN_NAME) deploy --app $(FLY_APP_NAME)
6-deploy-update:
	#  it will pull the latest version of Coder and deploy it.
	$(FLYCTL_BIN_NAME) deploy --app $(FLY_APP_NAME)
7-deploy-scale:
	# scale to 1 gb memory if oyu want.
	$(FLYCTL_BIN_NAME) scale memory 1024 --app $(FLY_APP_NAME)
8-deploy-sentry:
	# capture erros
	# https://fly.io/docs/reference/sentry/
	$(FLYCTL_BIN_NAME) ext sentry create
9-deploy-sentry-ls:
	# opens the sentry web gui to list and errors ...
	$(FLYCTL_BIN_NAME) apps errors

### Destroy 

10-deploy-destroy-all: 11-deploy-destroy-app 12-deploy-destroy-db
	# deletes the app and db
11-deploy-destroy-app:
	$(FLYCTL_BIN_NAME) postgres detach --app $(FLY_APP_NAME) $(FLY_APP_NAME_DB)
	$(FLYCTL_BIN_NAME) apps destroy $(FLY_APP_NAME)
12-deploy-destroy-db:
	$(FLYCTL_BIN_NAME) apps destroy $(FLY_APP_NAME_DB)

this is the .env file for config of deployment:

# The unique name of the app.
FLY_APP_NAME=temp-pemrify-server

# The unique name of your postres db.
FLY_APP_NAME_DB=temp-pemrify-server-postres

# The region you want to deploy to
# See a list of regions here: https://fly.io/docs/reference/regions/
FLY_APP_REGION=lhr

# The connection string that the server needs to connect to the postres db.
# This is generated at runtime, and so will update the .env at that time.
PG_CONNECTION_URL=DATABASE_URL=postgres://temp_server:utH7QQvcleHd7Mn@temp-pemrify -server-postres.flycast:5432/temp_ pemrify _server?sslmode=disable
@gedw99 gedw99 added the feature request New feature or request label Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants