Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
rework how the random integer is generated to hopefully better avoid …
Browse files Browse the repository at this point in the history
…collision. also increased the range to 30 seconds for the same reason.
  • Loading branch information
badarsebard committed Mar 4, 2022
1 parent 1a803e7 commit 3629a8e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HOSTNAME=local
NAMESPACE=badarsebard
NAME=xsoar
BINARY=terraform-provider-${NAME}
VERSION=0.3.14
VERSION=0.3.15
OS=${MY_OS}
ARCH=${MY_ARCH}

Expand Down
8 changes: 2 additions & 6 deletions xsoar/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package xsoar
import (
"context"
"crypto/tls"
"math/rand"
"net/http"
"os"
"time"

"github.com/badarsebard/xsoar-sdk-go/openapi"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"net/http"
"os"
)

var _ = os.Stderr
Expand Down Expand Up @@ -144,7 +141,6 @@ func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderReq
p.client = c
p.configured = true
p.data = &config
rand.Seed(time.Now().Unix())
}

// GetResources - Defines provider resources
Expand Down
32 changes: 11 additions & 21 deletions xsoar/resource_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"golang.org/x/crypto/ssh"
"hash/crc64"
"io"
"log"
"math/rand"
Expand Down Expand Up @@ -248,9 +249,13 @@ func (r resourceHost) Create(ctx context.Context, req tfsdk.CreateResourceReques
// 4) Check for lock
if !plan.NFSMount.Null {
// wait a random amount of time
randomTimeToWait := rand.Intn(10) + 1
crcTable := crc64.MakeTable(crc64.ISO)
seedInt := int64(crc64.Checksum([]byte(plan.Name.Value), crcTable))
randSource := rand.NewSource(seedInt)
nrand := rand.New(randSource)
randomTimeToWait := nrand.Intn(30) + 1
time.Sleep(time.Duration(randomTimeToWait))
// wait for a lock file in /tmp
// attempt to place lock
session, err = conn.NewSession()
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -260,32 +265,17 @@ func (r resourceHost) Create(ctx context.Context, req tfsdk.CreateResourceReques
return
}
defer session.Close()
err = session.Run(fmt.Sprintf(`while [[ -f "%s/xsoar_host_install.lock" ]]; do sleep %d; done`, plan.NFSMount.Value, randomTimeToWait))
err = session.Run(fmt.Sprintf(
`while [[ -f "%s/xsoar_host_install.lock" ]]; do sleep %d; done; sudo touch %s/xsoar_host_install.lock`,
plan.NFSMount.Value, randomTimeToWait, plan.NFSMount.Value,
))
if err != nil {
resp.Diagnostics.AddError(
"Error waiting for lock file",
"Lock file error: "+err.Error(),
)
return
}
// create lock file
session, err = conn.NewSession()
if err != nil {
resp.Diagnostics.AddError(
"Error creating ssh session",
"Could not create ssh session: "+err.Error(),
)
return
}
defer session.Close()
err = session.Run(fmt.Sprintf(`sudo touch %s/xsoar_host_install.lock`, plan.NFSMount.Value))
if err != nil {
resp.Diagnostics.AddError(
"Error creating lock file",
"Could not create lock file: "+err.Error(),
)
return
}
}

// 5) Execute installer
Expand Down

0 comments on commit 3629a8e

Please sign in to comment.