Created
February 7, 2022 23:40
-
-
Save cuducos/1a5649e87c33271deefb21f0197db817 to your computer and use it in GitHub Desktop.
Pulumi draft (Minha Receita)
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
#!/bin/bash | |
set -eux | |
# install postgres | |
sudo apt update -y | |
sudo apt install -y sudo postgresql-13 | |
sudo service postgresql start | |
# create postgres user and database | |
sudo -i -u postgres psql -U postgres -c "CREATE ROLE {{ .User }} WITH CREATEDB LOGIN PASSWORD '{{ .Pass }}'" | |
sudo -i -u postgres psql -U postgres -c "CREATE DATABASE {{ .DB }} OWNER {{ .User }}" | |
# expose port 5432 | |
PG_CONFIG=`sudo -i -u postgres psql postgres -c 'SHOW config_file' | grep 'postgresql.conf' | sed 's/ *//g'` | |
PG_HBA=`sudo -i -u postgres psql postgres -c 'SHOW hba_file' | grep 'pg_hba.conf' | sed 's/ *//g'` | |
echo "listen_addresses = '*'" >> $PG_CONFIG | |
echo "host all all 0.0.0.0/0 md5" >> "$PG_HBA" | |
echo "host all all ::/0 md5" >> "$PG_HBA" | |
# restart the server | |
sudo service postgresql restart |
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
package infra | |
import ( | |
"bytes" | |
"fmt" | |
"html/template" | |
"net/url" | |
"github.com/pulumi/pulumi-digitalocean/sdk/v4/go/digitalocean" | |
"github.com/pulumi/pulumi/sdk/v3/go/pulumi" | |
) | |
const ( | |
dropletSize = "s-4vcpu-8gb" | |
dropletImg = "debian-11-x64" | |
) | |
//go:embed bootstrap.sh | |
var bootstrapTemplate string | |
type pqConfig struct { | |
User string | |
Pass string | |
DB string | |
} | |
func loadConfig(s string) (*pqConfig, error) { | |
u, err := url.Parse(s) | |
if err != nil { | |
return nil, fmt.Errorf("error parsing posgres uri %s: %w", s, err) | |
} | |
user := u.User.Username() | |
pass, _ := u.User.Password() | |
c := pqConfig{user, pass, u.Path[1:]} | |
return &c, nil | |
} | |
func loadBootstrap(c *pqConfig) (string, error) { | |
t, err := template.New("bootstrap").Parse(bootstrapTemplate) | |
if err != nil { | |
return "", fmt.Errorf("error parsing bootstrap template: %w", err) | |
} | |
var b bytes.Buffer | |
if err = t.Execute(&b, c); err != nil { | |
return "", fmt.Errorf("error rendering bootstrap template: %w", err) | |
} | |
return b.String(), nil | |
} | |
func CreatePostgresSQL(dbURI string) error { | |
cfg, err := loadConfig(dbURI) | |
if err != nil { | |
return fmt.Errorf("error loading postgres configs: %w", err) | |
} | |
usrData, err := loadBootstrap(cfg) | |
if err != nil { | |
return fmt.Errorf("error loading bootstrap template: %w", err) | |
} | |
handler := func(ctx *pulumi.Context) error { | |
args := digitalocean.DropletArgs{ | |
Image: pulumi.String(dropletImg), // TODO: parameter | |
Region: pulumi.String("nyc2"), // TODO: parameter | |
Size: pulumi.String("s-1vcpu-1gb"), // TODO: use dropletSize | |
UserData: pulumi.String(usrData), | |
} | |
_, err := digitalocean.NewDroplet(ctx, "minha-receita-db-timestamp", &args) // TODO: timestamp | |
if err != nil { | |
return fmt.Errorf("error creating new droplet: %w", err) | |
} | |
fmt.Println("PostgreSQL instance created!") | |
return nil | |
} | |
pulumi.Run(handler) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment