2014-11-17 19:39:00 +00:00
/ *
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
Copyright 2014 The Perkeep Authors
2014-11-17 19:39:00 +00:00
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 .
* /
package main
import (
"bufio"
2017-11-26 09:05:38 +00:00
"context"
2014-11-17 19:39:00 +00:00
"flag"
"fmt"
"log"
2015-12-28 23:15:11 +00:00
"math/rand"
2014-11-17 19:39:00 +00:00
"os"
"strings"
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
"perkeep.org/pkg/cmdmain"
"perkeep.org/pkg/deploy/gce"
2014-11-17 19:39:00 +00:00
2015-11-26 23:31:36 +00:00
"go4.org/oauthutil"
2015-08-18 08:19:49 +00:00
"golang.org/x/oauth2"
2014-11-17 19:39:00 +00:00
)
type gceCmd struct {
project string
zone string
machine string
instName string
verbose bool
2017-01-13 15:04:23 +00:00
hostname string
wip bool
2014-11-17 19:39:00 +00:00
}
func init ( ) {
cmdmain . RegisterCommand ( "gce" , func ( flags * flag . FlagSet ) cmdmain . CommandRunner {
cmd := new ( gceCmd )
flags . StringVar ( & cmd . project , "project" , "" , "Name of Project." )
2015-12-28 23:15:11 +00:00
flags . StringVar ( & cmd . zone , "zone" , gce . DefaultRegion , "GCE zone or region. If a region is given, a random zone in that region is selected. See https://cloud.google.com/compute/docs/zones" )
flags . StringVar ( & cmd . machine , "machine" , gce . DefaultMachineType , "GCE machine type (e.g. n1-standard-1, f1-micro, g1-small); see https://cloud.google.com/compute/docs/machine-types" )
flags . StringVar ( & cmd . instName , "instance_name" , gce . DefaultInstanceName , "Name of VM instance." )
2017-01-13 15:04:23 +00:00
flags . StringVar ( & cmd . hostname , "hostname" , "" , "Optional hostname for the instance. If set, the custom metadata variable \"camlistore-hostname\" on the instance takes that value. Otherwise, camlistored sets that variable to the hostname we get from the camliNet DNS." )
flags . BoolVar ( & cmd . wip , "wip" , false , "Developer option. Deploy the WORKINPROGRESS camlistored tarball." )
2014-11-17 19:39:00 +00:00
flags . BoolVar ( & cmd . verbose , "verbose" , false , "Be verbose." )
return cmd
} )
}
const (
2017-01-13 15:04:23 +00:00
clientIdDat = "client-id.dat"
clientSecretDat = "client-secret.dat"
helpEnableAuth = ` Enable authentication: in your project console, navigate to "APIs and auth", "Credentials", click on "Create new Client ID", and pick "Installed application", with type "Other". Copy the CLIENT ID to ` + clientIdDat + ` , and the CLIENT SECRET to ` + clientSecretDat
helpCreateProject = "Go to " + gce . ConsoleURL + " to create a new Google Cloud project"
helpEnableAPIs = ` Enable the project APIs: in your project console, navigate to "APIs and auth", "APIs". In the list, enable "Google Cloud Storage", "Google Cloud Storage", "Google Cloud Storage JSON API", and "Google Compute Engine". `
2014-11-17 19:39:00 +00:00
)
func ( c * gceCmd ) Describe ( ) string {
2018-01-03 06:10:37 +00:00
return "Deploy Perkeep on Google Compute Engine."
2014-11-17 19:39:00 +00:00
}
func ( c * gceCmd ) Usage ( ) {
fmt . Fprintf ( os . Stderr , "Usage:\n\n %s\n %s\n\n" ,
2018-01-03 06:10:37 +00:00
"pk-deploy gce --project=<project> --hostname=<hostname> [options]" ,
"pk-deploy gce --project=<project> --cert=<cert file> --key=<key file> [options]" )
2014-11-17 19:39:00 +00:00
flag . PrintDefaults ( )
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
fmt . Fprintln ( os . Stderr , "\nTo get started:" )
2014-11-17 19:39:00 +00:00
printHelp ( )
}
func printHelp ( ) {
2017-01-13 15:04:23 +00:00
for _ , v := range [ ] string { helpCreateProject , helpEnableAuth , helpEnableAPIs } {
2014-11-17 19:39:00 +00:00
fmt . Fprintf ( os . Stderr , "%v\n" , v )
}
}
func ( c * gceCmd ) RunCommand ( args [ ] string ) error {
if c . verbose {
gce . Verbose = true
}
if c . project == "" {
return cmdmain . UsageError ( "Missing --project flag." )
}
2015-12-28 23:15:11 +00:00
// We embed the client ID and client secret, per
// https://developers.google.com/identity/protocols/OAuth2InstalledApp
// Notably: "The client ID and client secret obtained from the
// Developers Console are embedded in the source code of your
// application. In this context, the client secret is
// obviously not treated as a secret."
//
// These were created at:
// https://console.developers.google.com/apis/credentials?project=camlistore-website
// (Notes for Brad and Mathieu)
const (
clientID = "574004351801-9qqoggh6b5v3jqt722v43ikmgmtv60h3.apps.googleusercontent.com"
clientSecret = "Gf1zwaOcbJnRTE5zD4feKaTI" // NOT a secret, despite name
)
config := gce . NewOAuthConfig ( clientID , clientSecret )
2014-11-17 19:39:00 +00:00
config . RedirectURL = "urn:ietf:wg:oauth:2.0:oob"
2015-12-28 23:15:11 +00:00
hc := oauth2 . NewClient ( oauth2 . NoContext , oauth2 . ReuseTokenSource ( nil , & oauthutil . TokenSource {
Config : config ,
CacheFile : c . project + "-token.json" ,
AuthCode : func ( ) string {
fmt . Println ( "Get auth code from:" )
fmt . Printf ( "%v\n\n" , config . AuthCodeURL ( "my-state" , oauth2 . AccessTypeOffline , oauth2 . ApprovalForce ) )
fmt . Print ( "Enter auth code: " )
sc := bufio . NewScanner ( os . Stdin )
sc . Scan ( )
return strings . TrimSpace ( sc . Text ( ) )
} ,
} ) )
zone := c . zone
if gce . LooksLikeRegion ( zone ) {
region := zone
zones , err := gce . ZonesOfRegion ( hc , c . project , region )
if err != nil {
return err
}
if len ( zones ) == 0 {
return fmt . Errorf ( "no zones found in region %q; invalid region?" , region )
}
zone = zones [ rand . Intn ( len ( zones ) ) ]
}
2014-11-17 19:39:00 +00:00
instConf := & gce . InstanceConf {
Name : c . instName ,
Project : c . project ,
Machine : c . machine ,
2015-12-28 23:15:11 +00:00
Zone : zone ,
2014-11-17 19:39:00 +00:00
Hostname : c . hostname ,
2017-01-13 15:04:23 +00:00
WIP : c . wip ,
2014-11-17 19:39:00 +00:00
}
2015-12-28 23:15:11 +00:00
log . Printf ( "Creating instance %s (in project %s) in zone %s ..." , c . instName , c . project , zone )
2014-11-17 19:39:00 +00:00
depl := & gce . Deployer {
2015-12-28 23:15:11 +00:00
Client : hc ,
Conf : instConf ,
2014-11-17 19:39:00 +00:00
}
2015-12-28 23:15:11 +00:00
inst , err := depl . Create ( context . Background ( ) )
2014-11-17 19:39:00 +00:00
if err != nil {
return err
}
2015-12-28 23:15:11 +00:00
log . Printf ( "Instance created; starting up at %s" , inst . NetworkInterfaces [ 0 ] . AccessConfigs [ 0 ] . NatIP )
2014-11-17 19:39:00 +00:00
return nil
}