pax_global_header 0000666 0000000 0000000 00000000064 13357232042 0014513 g ustar 00root root 0000000 0000000 52 comment=ac834ce67862b7c4ed8bdbe86e664572f820c245
cloudsql-proxy-1.13/ 0000775 0000000 0000000 00000000000 13357232042 0014444 5 ustar 00root root 0000000 0000000 cloudsql-proxy-1.13/.gitignore 0000664 0000000 0000000 00000000026 13357232042 0016432 0 ustar 00root root 0000000 0000000 tests/cloud_sql_proxy
cloudsql-proxy-1.13/CONTRIBUTING.md 0000664 0000000 0000000 00000013466 13357232042 0016707 0 ustar 00root root 0000000 0000000 # Contributing
1. **Please sign one of the contributor license agreements below!**
1. Fork the repo, develop and test your code changes, add docs.
1. Make sure that your commit messages clearly describe the changes.
1. Send a pull request.
## Table of contents
* [Opening an issue](#opening-an-issue)
* [Contributor License Agreements](#contributor-license-agreements)
* [Contributor Code of Conduct](#contributor-code-of-conduct)
## Opening an issue
If you find a bug in the proxy code or an inaccuracy in the documentation,
please open an issue. GitHub provides a guide, [Mastering
Issues](https://guides.github.com/features/issues/), that is useful if you are
unfamiliar with the process. Here are the specific steps for opening an issue:
1. Go to the project issues page on GitHub.
1. Click the green `New Issue` button located in the upper right corner.
1. In the title field, write a single phrase that identifies your issue.
1. In the main editor, describe your issue.
1. Click the submit button.
Thank you. We will do our best to triage your issue within one business day, and
attempt to categorize your issues with an estimate of the priority and issue
type. We will try to respond with regular updates based on its priority:
* **Critical** respond and update daily, resolve with a week
* **High** respond and update weekly, resolve within six weeks
* **Medium** respond and update every three months, best effort resolution
* **Low** respond and update every six months, best effort resolution
The priority we assign will be roughly a function of the number of users we
expect to be impacted, as well as its severity. As a rule of thumb:
Severity |
Number of users |
Handful |
Some |
Most |
All |
Easy, obvious workaround |
Low |
Low |
Medium |
High
|
Non-obvious workaround available |
Low |
Medium |
High |
Critical |
Functionality blocked |
High |
High |
Critical |
Critical |
## Contributor License Agreements
Open-source software licensing is a wonderful arrangement that benefits
everyone, but in an imperfect world, we all need to exercise some legal
prudence. In order to protect you, Google, and most of all, everyone who comes
to depend on these libraries, we require that all contributors sign our short
and human-readable Contributor License Agreement (CLA). We don't want to open
the door to patent trolls, predatory lawyers, or anyone else who isn't on board
with creating value and making the world a better place. We hope you will agree
that the CLA offers very important protection and is easy to understand. Take a
moment to read it carefully, and if you agree with what you read, please sign it
now. If you believe you've already signed the appropriate CLA already for this
or any other Google open-source project, you shouldn't have to do so again. You
can review your signed CLAs at
[cla.developers.google.com/clas](https://cla.developers.google.com/clas).
First, check that you are signed in to a [Google
Account](https://accounts.google.com) that matches your [local Git email
address](https://help.github.com/articles/setting-your-email-in-git/). Then
choose one of the following:
* If you are **an individual writing original source code** and **you own the
intellectual property**, sign the [Individual
CLA](https://developers.google.com/open-source/cla/individual).
* If you work for **a company that wants to allow you to contribute**, sign the
[Corporate CLA](https://developers.google.com/open-source/cla/corporate).
You (and your authorized signer, if corporate) can sign the CLA
electronically. After that, we'll be able to accept your contributions.
## Contributor Code of Conduct
As contributors and maintainers of this project, and in the interest of
fostering an open and welcoming community, we pledge to respect all people who
contribute through reporting issues, posting feature requests, updating
documentation, submitting pull requests or patches, and other activities.
We are committed to making participation in this project a harassment-free
experience for everyone, regardless of level of experience, gender, gender
identity and expression, sexual orientation, disability, personal appearance,
body size, race, ethnicity, age, religion, or nationality.
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information, such as physical or electronic
addresses, without explicit permission
* Other unethical or unprofessional conduct.
Project maintainers have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct. By adopting this Code of Conduct, project
maintainers commit themselves to fairly and consistently applying these
principles to every aspect of managing this project. Project maintainers who do
not follow or enforce the Code of Conduct may be permanently removed from the
project team.
This code of conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community.
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by opening an issue or contacting one or more of the project
maintainers.
This Code of Conduct is adapted from the [Contributor
Covenant](http://contributor-covenant.org), version 1.2.0, available at
[http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
cloudsql-proxy-1.13/CONTRIBUTORS 0000664 0000000 0000000 00000002704 13357232042 0016327 0 ustar 00root root 0000000 0000000 # This is the official list of people who can contribute
# (and typically have contributed) code to the repository.
# The AUTHORS file lists the copyright holders; this file
# lists people. For example, Google employees are listed here
# but not in AUTHORS, because Google holds the copyright.
#
# The submission process automatically checks to make sure
# that people submitting code are listed in this file (by email address).
#
# Names should be added to this file only after verifying that
# the individual or the individual's organization has agreed to
# the appropriate Contributor License Agreement, found here:
#
# https://cla.developers.google.com/about/google-individual
# https://cla.developers.google.com/about/google-corporate
#
# The CLA can be filled out on the web:
#
# https://cla.developers.google.com/
#
# When adding J Random Contributor's name to this file,
# either J's name or J's organization's name should be
# added to the AUTHORS file, depending on whether the
# individual or corporate CLA was used.
# Names should be added to this file like so:
# Name
#
# An entry with two email addresses specifies that the
# first address should be used in the submit logs and
# that the second address should be recognized as the
# same person when interacting with Rietveld.
# Please keep the list sorted.
Mykola Smith
Frank van Rest
Kevin Malachowski
cloudsql-proxy-1.13/Kubernetes.md 0000664 0000000 0000000 00000010401 13357232042 0017071 0 ustar 00root root 0000000 0000000 Cloud SQL Proxy in a Kubernetes cluster
=======================================
The goal of this guide is to help you set-up and use Google Cloud SQL in
a Kubernetes cluster (GKE or not), through the Cloud SQL Proxy.
To make this as easy as possible, we will use the prepared docker image
so we can minimize the number of steps. No compilation needed!
Pre-requisites:
---------------
In order to set-up the Cloud SQL you will need,
- One or more Google Cloud SQL Databases. Refer to [the
documentation](https://cloud.google.com/sql/docs/) to create them.
- We will assume the name of the database instances are as follow:
`project:database1`, `project:database2`, etc.
- You need a service-account token with "Project Editor" privilegies,
and we will assume the file is in `$HOME/credentials.json`. Refer to
[the documentation](https://cloud.google.com/docs/authentication#developer_workflow)
to get the json credential file.
- Your `$HOME/.kube/config` points to your cluster and the namespace you
want to use.
Overview
--------
The recommended way to use the Cloud SQL Proxy in a Kubernetes cluster
is to use a TCP connection, as this allows the pod to be located on any
node. We will use [Kubernetes DNS
service](http://kubernetes.io/docs/admin/dns/) to connect to the proxy
seamlessly.
Setting-up the credentials
--------------------------
We need to create a secret to store the credentials that the Cloud Proxy
needs to connect to the project database instances:
```
kubectl create secret generic service-account-token --from-file=credentials.json=$HOME/credentials.json
```
Creating the Cloud SQL Proxy deployment
---------------------------------------
We need to create a deployment that will keep the Cloud SQL Proxy
container image alive.
Here is an example deployment file, `sqlproxy-deployment.yaml`:
```
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: cloudsqlproxy
spec:
replicas: 1
template:
metadata:
labels:
app: cloudsqlproxy
spec:
containers:
# Make sure to specify image tag in production
# Check out the newest version in release page
# https://github.com/GoogleCloudPlatform/cloudsql-proxy/releases
- image: b.gcr.io/cloudsql-docker/gce-proxy:latest
# 'Always' if imageTag is 'latest', else set to 'IfNotPresent'
imagePullPolicy: Always
name: cloudsqlproxy
command:
- /cloud_sql_proxy
- -dir=/cloudsql
- -instances=project:database1=tcp:0.0.0.0:3306,project:database2=tcp:0.0.0.0:3307
- -credential_file=/credentials/credentials.json
ports:
- name: port-database1
containerPort: 3306
- name: port-database2
containerPort: 3307
volumeMounts:
- mountPath: /cloudsql
name: cloudsql
- mountPath: /credentials
name: service-account-token
volumes:
- name: cloudsql
emptyDir:
- name: service-account-token
secret:
secretName: service-account-token
```
And then, create the deployment:
```
kubectl apply -f sqlproxy-deployment.yaml
```
This deployment will create pods that listen for connections on port
`3306` for `project:database1`, and `3307` for `project:database2`.
You can also change the number of replicas to increase availability.
Services to find the proxy
--------------------------
We can create services to find the pods. We have decided to use one
service per database to be able to select the database by name rather
than by port.
Create the services configuration, `sqlproxy-services.yaml`:
```
apiVersion: v1
kind: Service
metadata:
name: sqlproxy-service-database1
spec:
ports:
- port: 3306
targetPort: port-database1
selector:
app: cloudsqlproxy
---
apiVersion: v1
kind: Service
metadata:
name: sqlproxy-service-database2
spec:
ports:
- port: 3306
targetPort: port-database2
selector:
app: cloudsqlproxy
```
This will create two different services, `sqlproxy-service-database1`
and `sqlproxy-service-database2`.
Apply the configuration to create them:
```
kubectl apply -f sqlproxy-services.yaml
```
You can now connect using the same port `3306` to each database:
```
mysql --host=sqlproxy-service-database1 --port=3306 ...
mysql --host=sqlproxy-service-database2 --port=3306 ...
```
cloudsql-proxy-1.13/LICENSE 0000664 0000000 0000000 00000026136 13357232042 0015461 0 ustar 00root root 0000000 0000000
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
cloudsql-proxy-1.13/README.md 0000664 0000000 0000000 00000011670 13357232042 0015730 0 ustar 00root root 0000000 0000000
## Cloud SQL Proxy
The Cloud SQL Proxy allows a user with the appropriate permissions to connect
to a Second Generation Cloud SQL database without having to deal with IP whitelisting or SSL
certificates manually. It works by opening unix/tcp sockets on the local machine
and proxying connections to the associated Cloud SQL instances when the sockets
are used.
To build from source, ensure you have [go installed](https://golang.org/doc/install)
and have set [GOPATH](https://github.com/golang/go/wiki/GOPATH). Then, simply do a go get:
go get github.com/GoogleCloudPlatform/cloudsql-proxy/cmd/cloud_sql_proxy
The cloud_sql_proxy will be placed in $GOPATH/bin after go get completes.
cloud_sql_proxy takes a few arguments to configure:
* `-fuse`: requires access to `/dev/fuse` as well as the `fusermount` binary. An
optional `-fuse_tmp` flag can specify where to place temporary files. The
directory indicated by `-dir` is mounted.
* `-instances="project1:region:instance1,project3:region:instance1"`: A comma-separated list
of instances to open inside `-dir`. Also supports exposing a tcp port instead of using Unix Domain Sockets; see examples below.
Same list can be provided via INSTANCES environment variable, in case when both are provided - proxy will use command line flag.
* `-instances_metadata=metadata_key`: Usable on [GCE](https://cloud.google.com/compute/docs/quickstart) only. The given [GCE metadata](https://cloud.google.com/compute/docs/metadata) key will be
polled for a list of instances to open in `-dir`. The format for the value is the same as the 'instances' flag. A hanging-poll strategy is used, meaning that changes to
the metadata value will be reflected in the `-dir` even while the proxy is
running. When an instance is removed from the list the corresponding socket
will be removed from `-dir` as well (unless it was also specified in
`-instances`), but any existing connections to this instance will NOT be
terminated.
Note: `-instances` and `-instances_metadata` may be used at the same time but
are not compatible with the `-fuse` flag.
By default, the proxy will authenticate under the default service account of the
Compute Engine VM it is running on. Therefore, the VM must have at least the
sqlservice.admin API scope ("https://www.googleapis.com/auth/sqlservice.admin")
and the associated project must have the SQL Admin API
enabled. The default service account must also have at least WRITER/EDITOR
priviledges to any projects of target SQL instances.
Specifying the `-credential_file` flag allows use of the proxy outside of
Google's cloud. Simply [create a new service
account](https://cloud.google.com/sql/docs/mysql/sql-proxy#create-service-account),
download the associated JSON file, and set `-credential_file` to the path of the
JSON file. You can also set the GOOGLE_APPLICATION_CREDENTIALS environment variable
instead of passing this flag.
## Example invocations:
./cloud_sql_proxy -dir=/cloudsql -instances=my-project:us-central1:sql-inst &
mysql -u root -S /cloudsql/my-project:us-central1:sql-inst
# For -fuse you do not need to specify instance names ahead of time:
./cloud_sql_proxy -dir=/cloudsql -fuse &
mysql -u root -S /cloudsql/my-project:us-central1:sql-inst
# For programs which do not support using Unix Domain Sockets, specify tcp:
./cloud_sql_proxy -dir=/cloudsql -instances=my-project:us-central1:sql-inst=tcp:3306 &
mysql -u root -h 127.0.0.1
## To use inside a Go program:
If your program is written in [Go](https://golang.org) you can use the Cloud SQL Proxy as a library,
avoiding the need to start the Proxy as a companion process.
### MySQL
If you're using the the MySQL [go-sql-driver](https://github.com/go-sql-driver/mysql)
you can use helper functions found in the [`proxy/dialers/mysql` package](https://godoc.org/github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql). See [example usage](https://github.com/GoogleCloudPlatform/cloudsql-proxy/blob/master/tests/dialers_test.go).
### Postgres
If you're using the the Postgres [lib/pq](https://github.com/lib/pq), you can use the `cloudsqlpostgres` driver from [here](https://github.com/GoogleCloudPlatform/cloudsql-proxy/tree/master/proxy/dialers/postgres). See [example usage](https://github.com/GoogleCloudPlatform/cloudsql-proxy/blob/master/proxy/dialers/postgres/hook_test.go).
I'm open to adding more drivers, feel free to file an issue.
## To use from Kubernetes:
### Deploying Cloud SQL Proxy as a sidecar container
Follow this [page](https://github.com/GoogleCloudPlatform/kubernetes-engine-samples/tree/master/cloudsql). See also
[Connecting from Google Kubernetes Engine](https://cloud.google.com/sql/docs/mysql/connect-kubernetes-engine).
### Deploy Cloud SQL Proxy as a Cluster Service using Helm
Follow this [instruction](https://github.com/kubernetes/charts/tree/master/stable/gcloud-sqlproxy).
This chart creates a Deployment and a Service, but we recommend deploying the proxy as a sidecar container in your pods.
cloudsql-proxy-1.13/cmd/ 0000775 0000000 0000000 00000000000 13357232042 0015207 5 ustar 00root root 0000000 0000000 cloudsql-proxy-1.13/cmd/cloud_sql_proxy/ 0000775 0000000 0000000 00000000000 13357232042 0020435 5 ustar 00root root 0000000 0000000 cloudsql-proxy-1.13/cmd/cloud_sql_proxy/build.sh 0000775 0000000 0000000 00000006353 13357232042 0022102 0 ustar 00root root 0000000 0000000 #!/bin/bash
#
# build.sh wraps `go build` to make compiling the Cloud SQL Proxy for
# distribution more streamlined. When doing normal development on the proxy,
# `go build .` (or even `go run .`) is sufficient for iterating on the code.
# This script simply allows a convenient way to cross compile and build a docker
# container.
#
# With no arguments, this script will build a binary marked with "development",
# otherwise the binary version will be annotated with the string provided.
#
# # Build a binary labeled with 'development'
# ./build.sh
#
# # Build a binary labeled with 'beta'
# ./build.sh beta
#
# Specifying 'release' as the first argument to this script will cross compile
# for all supported operating systems and architectures. This requires a version
# identifier to be supplied as the second argument:
#
# # Build a binary for each of the supported platforms labeled with '0.001'
# ./build.sh release 0.001
#
# Specifying docker as the first argument to this script will build a
# container, tagging it with the identifier in the second argument.
#
# # Build a docker container named 'cloud-sql-proxy:my-tag'
# ./build docker my-tag
files=$(git status -s)
if [[ $? != 0 ]]; then
echo >&2 "Error running git status"
exit 2
fi
# build builds a new binary. Expected variables:
# VERSION: string to print out when --version is passed to the final binary
# OS: operation system to target (windows, darwin, linux, etc)
# ARCH: architecture to target (amd64, 386, etc)
# OUT: location to place binary
build() {
echo " Compile -> $OUT"
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build \
-ldflags "-X 'main.versionString=$VERSION'" -a -installsuffix cgo -o $OUT \
github.com/GoogleCloudPlatform/cloudsql-proxy/cmd/cloud_sql_proxy
}
# git_version echos out version information related to the git repo and date.
git_version() {
edits=""
if [[ "$files" != "" ]]; then
edits=" (local edits)"
fi
echo "sha $(git rev-parse HEAD)$edits built $(date)"
}
set -e
case $1 in
"release")
if [[ "$files" != "" ]]; then
echo >&2 "Can't build a release version with local edits; files:"
echo >&2 "$files"
exit 1
fi
if [[ "$2" == "" ]]; then
echo >&2 "Must provide a version number to use as the second parameter:"
echo >&2 " $0 release my-version-string"
exit 1
fi
VERSION="version $2; $(git_version)"
echo "Cross-compiling $VERSION..."
for OS in windows darwin linux; do
for ARCH in amd64 386; do
OUT="cloud_sql_proxy.$OS.$ARCH"
build
done
done
;;
"docker")
if [[ "$2" == "" ]]; then
echo >&2 "Must provide a version number to use as the second parameter"
echo >&2 " $0 docker my-version-string"
exit 1
fi
VERSION="version $2; $(git_version)"
OS="linux"
ARCH="amd64"
OUT=cloud_sql_proxy.docker
echo "Compiling $VERSION for docker..."
build
cat >Dockerfile <