Развертывание частного приложения github repo golang на эластичном стебле - PullRequest
0 голосов
/ 26 апреля 2018

Я пытался развернуть приложение Golang на AWS EB в течение нескольких дней.

Я пытаюсь развернуть мое приложение на сервере EB Preconfigured Docker - Go 1.4 running on 64bit Debian/2.9.2 с помощью eb cli с помощью команды eb deploy в папке моего приложения.

Через пару секунд у меня появляется сообщение об ошибке, в котором говорится, что мое приложение не было развернуто из-за ошибки. Глядя на eb-activity.log, вот что я вижу:

    /var/log/eb-activity.log
    -------------------------------------
    Fetching https://golang.org/x/crypto?go-get=1
    Parsing meta tags from https://golang.org/x/crypto?go-get=1 (status code 200)
    golang.org/x/crypto (download)
    Fetching https://golang.org/x/sys/unix?go-get=1
    Parsing meta tags from https://golang.org/x/sys/unix?go-get=1 (status code 200)
    get "golang.org/x/sys/unix": found meta tag main.metaImport{Prefix:"golang.org/x/sys", VCS:"git", RepoRoot:"https://go.googlesource.com/sys"} at https://golang.org/x/sys/unix?go-get=1
    get "golang.org/x/sys/unix": verifying non-authoritative meta tag
    Fetching https://golang.org/x/sys?go-get=1
    Parsing meta tags from https://golang.org/x/sys?go-get=1 (status code 200)
    golang.org/x/sys (download)
    github.com/randomuser/private-repo (download)
    # cd .; git clone https://github.com/randomuser/private-repo /go/src/github.com/randomuser/private-repo
    Cloning into '/go/src/github.com/randomuser/private-repo'...
    fatal: could not read Username for 'https://github.com': No such device or address
    package github.com/Sirupsen/logrus
            imports golang.org/x/crypto/ssh/terminal
            imports golang.org/x/sys/unix
            imports github.com/randomuser/private-repo/apis: exit status 128
    package github.com/Sirupsen/logrus
            imports golang.org/x/crypto/ssh/terminal
            imports golang.org/x/sys/unix
            imports github.com/randomuser/private-repo/app
            imports github.com/randomuser/private-repo/app
            imports github.com/randomuser/private-repo/app: cannot find package "github.com/randomuser/private-repo/app" in any of:
            /usr/src/go/src/github.com/randomuser/private-repo/app (from $GOROOT)
            /go/src/github.com/randomuser/private-repo/app (from $GOPATH)

Я полагаю, что существует проблема, когда сервер пытается установить приложение, похоже, он пытается получить данные из моего частного репозитория на github ...

Я ссылался на свои подпакеты приложения как github.com/randomuser/private-repo/subpackage Я предположил, что именно поэтому он ведет себя так.

Есть ли способ развернуть весь мой код, заставив мое частное репо быть заполненным в GOROOT src / github.com / randomuser / private-repo /, чтобы сервер не пытался его получить?

Я не нашел ни одного правильного примера ни в Amazon docs (приложениях с несколькими пакетами), ни в Github.

Я что-то упустил? Есть ли лучшее решение?

В дополнение к этому, я также попытался развернуть мой скомпилированный двоичный файл напрямую (создать папку, в которую я помещаю только двоичный файл, заархивировать его и загрузить в enb-файл ebs), но он тоже не сработал ... Может быть, это опция требует еще один конфиг env (если да, то какой?).

Спасибо за вашу помощь :)

Конфигурация

  • Приложение Golang, имеющее следующие папки:

    ├── Dockerfile
    ├── server.go
    ├── Gopkg.lock
    ├── Gopkg.toml
    ├── Makefile
    ├── apis
    │   ├── auth.go
    │   ├── auth_test.go
    │   ├── ...
    ├── app
    │   ├── config.go
    │   ├── init.go
    │   ├── logger.go
    │   ├── scope.go
    │   ├── transactional.go
    │   └── version.go
    ├── config
    │   ├── dev.app.yaml
    │   ├── errors.yaml
    │   └── prod.app.yaml
    ├── daos
    │   ├── auth.go
    │   ├── auth_test.go
    │   ├── ...
    ├── errors
    │   ├── api_error.go
    │   ├── api_error_test.go
    │   ├── errors.go
    │   ├── errors_test.go
    │   ├── template.go
    │   └── template_test.go
    ├── models
    │   ├── identity.go
    │   ├── ...
    ├── services
    │   ├── auth.go
    │   ├── auth_test.go
    │   ├── ...
    ├── util
    │   ├── paginated_list.go
    │   └── paginated_list_test.go
    
  • Вот содержимое моего сервера.

    package main
    
    import (
        "flag"
        "fmt"
        "net/http"
    
        "github.com/jinzhu/gorm"
        _ "github.com/jinzhu/gorm/dialects/mysql"
    
        "github.com/randomuser/private-repo/apis"
        "github.com/randomuser/private-repo/app"
        "github.com/randomuser/private-repo/daos"
        "github.com/randomuser/private-repo/errors"
        "github.com/randomuser/private-repo/services"
    )
    
    func main() {
        // getting env from command line
        // env is either prod, preprod or dev
        // by default, env is prod
        env := flag.String("env", "prod", "environment: prod, preprod or dev")
        flag.Parse()
    
        ...
        router.To("GET,HEAD", "/ping", func(c *routing.Context) error {
           c.Abort() // skip all other middlewares/handlers
           return c.Write("OK " + app.Version)
        })
        ...
        // Serve on port 5000
    
  • Содержимое My Dockerfile:

    FROM golang:1.4.2-onbuild
    
    ADD . /go/src/github.com/randomuser/private-repo
    RUN go install github.com/randomuser/private-repo
    
    EXPOSE 5000
    
    ENTRYPOINT /go/bin/private-repo
    

1 Ответ

0 голосов
/ 08 мая 2018

Мне, наконец, удалось заставить его работать.

Итак, я создал новое приложение eb (без докера).Затем я понял, что мое приложение не может каким-то образом получить переменные env, установленные в консоли ... Итак, я сделал так, что я принудительно передал переменные env моему приложению во время запуска из моего производственного конфигурационного файла, используяСценарий build.sh см. ниже:

#!/bin/bash -xe
# See http://tldp.org/LDP/abs/html/options.html
# -x -> Print each command to stdout before executing it, expand commands
# -e -> Abort script at first error, when a command exits with non-zero status
#   (except in until or while loops, if-tests, list constructs)

# $GOPATH isn't set by default, nor do we have a usable Go workspace :'(
GOPATH="/var/app/current"
APP_BUILD_DIR="$GOPATH/src/to-be-defined"     # We will build the app here
APP_STAGING_DIR="/var/app/staging"            # Current directory
DEP_VERSION="v0.3.2"                          # Use specific version for stability
ENV_VAR_PREFIX="TO_BE_DEFINED_"

# Install dep, a Go dependency management tool, if not already installed or if
# the version does not match.
if ! hash dep 2> /dev/null ||\
    [[ $(dep version | awk 'NR==2{print $3}') != "$DEP_VERSION" ]]; then
    # /usr/local/bin is expected to be on $PATH.
    curl -L \
        -s https://github.com/golang/dep/releases/download/$DEP_VERSION/dep-linux-amd64 \
        -o /usr/local/bin/dep

    chmod +x /usr/local/bin/dep
fi

# Remove the $APP_BUILD_DIR just in case it was left behind in a failed build.
rm -rf $APP_BUILD_DIR

# Setup the application directory
mkdir -p $APP_BUILD_DIR

# mv all files to $APP_BUILD_DIR
# https://superuser.com/questions/62141/how-to-move-all-files-from-current-directory-to-upper-directory
mv * .[^.]* $APP_BUILD_DIR
cd $APP_BUILD_DIR

# Pull in dependencies into vendor/.
dep ensure

# Build the binary with jsoniter tag.
go build -o application -tags=jsoniter .

# Modify permissons to make the binary executable.
chmod +x application
# Move the binary back to staging dir.
# Along with the configuration files.
mkdir $APP_STAGING_DIR/bin
# By default, `bin/application` is executed. This way, a Procfile isn't needed.
mv application $APP_STAGING_DIR/bin
cp -r config $APP_STAGING_DIR
# TODO: Fix the viper not working with env var
# Generate prod config from env variables
/opt/elasticbeanstalk/bin/get-config environment --output YAML | sed s/${ENV_VAR_PREFIX}//g > $APP_STAGING_DIR/config/prod.app.yaml
# Copy .ebextensions back to staging directory.
# cp -r .ebextensions $APP_STAGING_DIR

# Clean up.
rm -rf $APP_BUILD_DIR

echo "Build successful!!"

Мой файл build.sh вызывается EBS с использованием этого файла сборки:

make: ./build.sh

Et voilà!Теперь все работает правильно:)

...