|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + v1 "github.com/onepanelio/core/pkg" |
| 6 | + "github.com/pressly/goose" |
| 7 | + "log" |
| 8 | +) |
| 9 | + |
| 10 | +const cvatWorkspaceTemplate = `# Docker containers that are part of the Workspace |
| 11 | +containers: |
| 12 | +- name: cvat-db |
| 13 | + image: postgres:10-alpine |
| 14 | + env: |
| 15 | + - name: POSTGRES_USER |
| 16 | + value: root |
| 17 | + - name: POSTGRES_DB |
| 18 | + value: cvat |
| 19 | + - name: POSTGRES_HOST_AUTH_METHOD |
| 20 | + value: trust |
| 21 | + - name: PGDATA |
| 22 | + value: /var/lib/psql/data |
| 23 | + ports: |
| 24 | + - containerPort: 5432 |
| 25 | + name: tcp |
| 26 | + volumeMounts: |
| 27 | + - name: db |
| 28 | + mountPath: /var/lib/psql |
| 29 | +- name: cvat-redis |
| 30 | + image: redis:4.0-alpine |
| 31 | + ports: |
| 32 | + - containerPort: 6379 |
| 33 | + name: tcp |
| 34 | +- name: cvat |
| 35 | + image: onepanel/cvat:v0.6.23 |
| 36 | + command: ["/bin/bash", "-c"] |
| 37 | + args: ["/usr/bin/supervisord && /usr/bin/python3 ~/manage.py shell --command='import os;from django.contrib.auth.models import User;u = User(username=os.getenv('DJANGO_SUPERUSER_USERNAME','admin'));u.set_password(os.getenv('DJANGO_SUPERUSER_PASSWORD','admin'));u.is_superuser = True;u.is_staff = True;u.save();'"] |
| 38 | + env: |
| 39 | + - name: DJANGO_MODWSGI_EXTRA_ARGS |
| 40 | + value: "" |
| 41 | + - name: ALLOWED_HOSTS |
| 42 | + value: '*' |
| 43 | + - name: CVAT_REDIS_HOST |
| 44 | + value: localhost |
| 45 | + - name: CVAT_POSTGRES_HOST |
| 46 | + value: localhost |
| 47 | + - name: CVAT_SHARE_URL |
| 48 | + value: /home/django/data |
| 49 | + ports: |
| 50 | + - containerPort: 8080 |
| 51 | + name: http |
| 52 | + volumeMounts: |
| 53 | + - name: data |
| 54 | + mountPath: /home/django/data |
| 55 | + - name: keys |
| 56 | + mountPath: /home/django/keys |
| 57 | + - name: logs |
| 58 | + mountPath: /home/django/logs |
| 59 | + - name: models |
| 60 | + mountPath: /home/django/models |
| 61 | +- name: cvat-ui |
| 62 | + image: onepanel/cvat-ui:v0.6.23 |
| 63 | + ports: |
| 64 | + - containerPort: 80 |
| 65 | + name: http |
| 66 | +ports: |
| 67 | +- name: cvat-ui |
| 68 | + port: 80 |
| 69 | + protocol: TCP |
| 70 | + targetPort: 80 |
| 71 | +- name: cvat |
| 72 | + port: 8080 |
| 73 | + protocol: TCP |
| 74 | + targetPort: 8080 |
| 75 | +routes: |
| 76 | +- match: |
| 77 | + - uri: |
| 78 | + regex: /api/.*|/git/.*|/tensorflow/.*|/auto_annotation/.*|/analytics/.*|/static/.*|/admin/.*|/documentation/.*|/dextr/.*|/reid/.* |
| 79 | + - queryParams: |
| 80 | + id: |
| 81 | + regex: \d+.* |
| 82 | + route: |
| 83 | + - destination: |
| 84 | + port: |
| 85 | + number: 8080 |
| 86 | +- match: |
| 87 | + - uri: |
| 88 | + prefix: / |
| 89 | + route: |
| 90 | + - destination: |
| 91 | + port: |
| 92 | + number: 80 |
| 93 | +postExecutionWorkflow: |
| 94 | + entrypoint: main |
| 95 | + templates: |
| 96 | + - name: main |
| 97 | + dag: |
| 98 | + tasks: |
| 99 | + - name: slack-notify |
| 100 | + template: slack-notify |
| 101 | + - name: slack-notify |
| 102 | + container: |
| 103 | + image: technosophos/slack-notify |
| 104 | + args: |
| 105 | + - SLACK_USERNAME=onepanel SLACK_TITLE="Your workspace is ready" SLACK_ICON=https://www.gravatar.com/avatar/5c4478592fe00878f62f0027be59c1bd SLACK_MESSAGE="Your workspace is now running" ./slack-notify |
| 106 | + command: |
| 107 | + - sh |
| 108 | + - -c |
| 109 | +` |
| 110 | + |
| 111 | +const cvatTemplateName = "cvat" |
| 112 | + |
| 113 | +func init() { |
| 114 | + goose.AddMigration(Up20200528140124, Down20200528140124) |
| 115 | +} |
| 116 | + |
| 117 | +func Up20200528140124(tx *sql.Tx) error { |
| 118 | + // This code is executed when the migration is applied. |
| 119 | + //time.Sleep(2 * time.Second) |
| 120 | + |
| 121 | + client, err := getClient() |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + namespaces, err := client.ListOnepanelEnabledNamespaces() |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + workspaceTemplate := &v1.WorkspaceTemplate{ |
| 132 | + Name: cvatTemplateName, |
| 133 | + Manifest: cvatWorkspaceTemplate, |
| 134 | + } |
| 135 | + |
| 136 | + for _, namespace := range namespaces { |
| 137 | + if _, err := client.CreateWorkspaceTemplate(namespace.Name, workspaceTemplate); err != nil { |
| 138 | + log.Printf("error %v", err.Error()) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func Down20200528140124(tx *sql.Tx) error { |
| 146 | + // This code is executed when the migration is rolled back. |
| 147 | + |
| 148 | + client, err := getClient() |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + namespaces, err := client.ListOnepanelEnabledNamespaces() |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + for _, namespace := range namespaces { |
| 159 | + if err := client.DeleteWorkspace(namespace.Name, cvatTemplateName); err != nil { |
| 160 | + log.Printf("error %v", err.Error()) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
0 commit comments