Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions windows-builder/builder/builder/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ const (

// Server encapsulates a GCE Instance.
type Server struct {
context *context.Context
projectID string
service *compute.Service
instance *compute.Instance
context *context.Context
projectID string
vpcProjectID string
service *compute.Service
instance *compute.Instance
Remote
}

Expand Down Expand Up @@ -72,7 +73,8 @@ func NewServer(ctx context.Context, bs *BuilderServer) *Server {
log.Fatalf("Cannot create new server without project ID: %+v", err)
return nil
}
s := &Server{projectID: projectID}
s := &Server{projectID: projectID, vpcProjectID: *bs.NetworkProjectId}
log.Printf("ProjectID: %s, VPCProjectID: %s", s.projectID, s.vpcProjectID)

log.Printf("Starting GCE service in project %s", projectID)
err = s.newGCEService(ctx)
Expand Down Expand Up @@ -169,6 +171,15 @@ func (s *Server) newInstance(bs *BuilderServer) error {
}
}

var projectNetwork string
if s.vpcProjectID == "" {
projectNetwork = s.projectID
} else {
projectNetwork = s.vpcProjectID
}

log.Printf("Project Network: %s", projectNetwork)

instance := &compute.Instance{
Name: name,
MachineType: prefix + s.projectID + "/zones/" + *bs.Zone + "/machineTypes/" + machineType,
Expand Down Expand Up @@ -196,8 +207,8 @@ func (s *Server) newInstance(bs *BuilderServer) error {
NetworkInterfaces: []*compute.NetworkInterface{
&compute.NetworkInterface{
AccessConfigs: accessConfigs,
Network: prefix + s.projectID + "/global/networks/" + *bs.VPC,
Subnetwork: prefix + s.projectID + "/regions/" + *bs.Region + "/subnetworks/" + *bs.Subnet,
Network: prefix + projectNetwork + "/global/networks/" + *bs.VPC,
Subnetwork: prefix + projectNetwork + "/regions/" + *bs.Region + "/subnetworks/" + *bs.Subnet,
},
},
ServiceAccounts: []*compute.ServiceAccount{
Expand All @@ -212,7 +223,7 @@ func (s *Server) newInstance(bs *BuilderServer) error {
Scheduling: &compute.Scheduling{
Preemptible: *bs.Preemptible,
},
Tags: &compute.Tags {
Tags: &compute.Tags{
Items: bs.GetTags(),
},
}
Expand Down Expand Up @@ -261,7 +272,7 @@ func (s *Server) DeleteInstance(bs *BuilderServer) error {
}

// getInternalIP gets an internal IP for an instance.
func(s *Server) getInternalIP(bs *BuilderServer) (string, error) {
func (s *Server) getInternalIP(bs *BuilderServer) (string, error) {
err := s.refreshInstance(bs)
if err != nil {
log.Printf("Error refreshing instance: %+v", err)
Expand Down Expand Up @@ -291,7 +302,16 @@ func (s *Server) getExternalIP(bs *BuilderServer) (string, error) {

// setFirewallRule allows ingress on WinRM port.
func (s *Server) setFirewallRule(bs *BuilderServer) error {
list, err := s.service.Firewalls.List(s.projectID).Do()
var projectNetwork string
if s.vpcProjectID == "" {
projectNetwork = s.projectID
} else {
projectNetwork = s.vpcProjectID
}

log.Printf("Project Network in Firewall: %s", projectNetwork)

list, err := s.service.Firewalls.List(projectNetwork).Do()
if err != nil {
log.Printf("Could not list GCE firewalls: %+v", err)
return err
Expand All @@ -305,25 +325,25 @@ func (s *Server) setFirewallRule(bs *BuilderServer) error {

firewallRule := &compute.Firewall{
Allowed: []*compute.FirewallAllowed{
&compute.FirewallAllowed{
{
IPProtocol: "tcp",
Ports: []string{"5986"},
},
},
Direction: "INGRESS",
Name: "allow-winrm-ingress",
SourceRanges: []string{"0.0.0.0/0"},
Network: prefix + s.projectID + "/global/networks/" + *bs.VPC,
Network: prefix + projectNetwork + "/global/networks/" + *bs.VPC,
}
_, err = s.service.Firewalls.Insert(s.projectID, firewallRule).Do()
_, err = s.service.Firewalls.Insert(projectNetwork, firewallRule).Do()
if err != nil {
log.Printf("Error setting firewall rule: %v", err)
return err
}
return nil
}

//WindowsPasswordConfig stores metadata to be sent to GCE.
// WindowsPasswordConfig stores metadata to be sent to GCE.
type WindowsPasswordConfig struct {
key *rsa.PrivateKey
password string
Expand All @@ -334,7 +354,7 @@ type WindowsPasswordConfig struct {
ExpireOn time.Time `json:"expireOn"`
}

//WindowsPasswordResponse stores data received from GCE.
// WindowsPasswordResponse stores data received from GCE.
type WindowsPasswordResponse struct {
UserName string `json:"userName"`
PasswordFound bool `json:"passwordFound"`
Expand Down
39 changes: 20 additions & 19 deletions windows-builder/builder/builder/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ type Remote struct {
}

type BuilderServer struct {
ImageUrl *string
VPC *string
Subnet *string
Region *string
Zone *string
Labels *string
MachineType *string
Preemptible *bool
DiskSizeGb *int64
DiskType *string
ServiceAccount *string
Tags *string
UseInternalNet *bool
CreateExternalIP *bool
ImageUrl *string
VPC *string
Subnet *string
Region *string
Zone *string
Labels *string
MachineType *string
Preemptible *bool
DiskSizeGb *int64
DiskType *string
ServiceAccount *string
Tags *string
UseInternalNet *bool
CreateExternalIP *bool
NetworkProjectId *string
}

// Wait for server to be available.
Expand Down Expand Up @@ -199,7 +200,7 @@ func (bs *BuilderServer) GetLabelsMap() map[string]string {
if *bs.Labels == "" {
return nil
}

var labelsMap map[string]string

for _, label := range strings.Split(*bs.Labels, ",") {
Expand All @@ -225,12 +226,12 @@ func (bs *BuilderServer) GetLabelsMap() map[string]string {
}

func (bs *BuilderServer) GetTags() []string {
if *bs.Tags == "" {
return nil
}
if *bs.Tags == "" {
return nil
}

var tags []string
for _, tag := range strings.Split(*bs.Tags, ",") {
for _, tag := range strings.Split(*bs.Tags, ",") {
tags = append(tags, strings.TrimSpace(tag))
}
return tags
Expand Down
8 changes: 5 additions & 3 deletions windows-builder/builder/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"os/signal"
"context"
"syscall"
"flag"
"log"
"os"
"os/signal"
"syscall"

"github.com/GoogleCloudPlatform/cloud-builders-community/windows-builder/builder/builder"
)
Expand All @@ -19,7 +19,7 @@ var (
notCopyWorkspace = flag.Bool("not-copy-workspace", false, "If copy workspace or not")
workspacePath = flag.String("workspace-path", "/workspace", "The directory to copy data from")
workspaceBucket = flag.String("workspace-bucket", "", "The bucket to copy the directory to. Defaults to {project-id}_cloudbuild")
image = flag.String("image", "windows-cloud/global/images/windows-server-2019-dc-for-containers-v20191210", "Windows image to start the server from")
image = flag.String("image", "windows-cloud/global/images/windows-2019", "Windows image to start the server from")
network = flag.String("network", "default", "The VPC name to use when creating the Windows server")
subnetwork = flag.String("subnetwork", "default", "The Subnetwork name to use when creating the Windows server")
region = flag.String("region", "us-central1", "The region name to use when creating the Windows server")
Expand All @@ -35,6 +35,7 @@ var (
tags = flag.String("tags", "", "List of strings eparated by comma to add when creating the Windows server")
useInternalNet = flag.Bool("use-internal-network", false, "Communicate with Windows server over the internal network")
createExternalIP = flag.Bool("create-external-ip", false, "Create an external IP address when using internal network")
networkProjectId = flag.String("networkProjectId", "", "Use when you have a network in a different project")
)

func main() {
Expand Down Expand Up @@ -69,6 +70,7 @@ func main() {
Tags: tags,
UseInternalNet: useInternalNet,
CreateExternalIP: createExternalIP,
NetworkProjectId: networkProjectId,
}
s = builder.NewServer(ctx, bs)
r = &s.Remote
Expand Down