我一直在努力尝试将我的Golang应用程序部署到AWS EB几天。
我正尝试使用eb cli通过我的app文件夹中的Preconfigured Docker - Go 1.4 running on 64bit Debian/2.9.2
命令在EB服务器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上的私人仓库中检索...
我将我的app子包引用为github.com/randomuser/private-repo/subpackage
我认为这就是为什么它的行为。
有没有办法部署我的所有代码,强制我的私人仓库在GOROOT src / github.com / randomuser / private-repo /中填充,所以服务器不必尝试获取它?
我没有从亚马逊文档(多包应用程序)或Github中找到任何正确的示例。
我错过了什么吗?有更好的解决方案吗?
另一方面,我也尝试直接部署我编译的二进制文件(创建一个文件夹,我只放置二进制文件,压缩它并将其上传到ebs env)但是它既没有用...也许这个选项需要另一个env配置(如果是,哪一个?)。
感谢您的帮助:)
配置
Golang app具有以下文件夹:
├── 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
以下是我的server.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
我的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
答案 0 :(得分:3)
我终于成功了。
所以我创建了一个全新的eb应用程序(没有docker)。 然后我发现我的应用程序无法以某种方式检索控制台中设置的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使用此Buildfile调用:
make: ./build.sh
Etvoilà!现在一切正常了:))