@@ -23,7 +23,6 @@ import (
23
23
"github.com/docker/infrakit/pkg/template"
24
24
"github.com/docker/infrakit/pkg/types"
25
25
"github.com/docker/infrakit/pkg/util/exec"
26
- "github.com/nightlyone/lockfile"
27
26
"github.com/spf13/afero"
28
27
)
29
28
@@ -63,7 +62,7 @@ var instNameRegex = regexp.MustCompile("(.*)(instance-[0-9]+)")
63
62
type plugin struct {
64
63
Dir string
65
64
fs afero.Fs
66
- fsLock lockfile. Lockfile
65
+ fsLock sync. Mutex
67
66
applying bool
68
67
applyLock sync.Mutex
69
68
pretend bool // true to actually do terraform apply
@@ -98,14 +97,10 @@ type ImportOptions struct {
98
97
// NewTerraformInstancePlugin returns an instance plugin backed by disk files.
99
98
func NewTerraformInstancePlugin (dir string , pollInterval time.Duration , standalone bool , envs []string , importOpts * ImportOptions ) instance.Plugin {
100
99
log .Debugln ("terraform instance plugin. dir=" , dir )
101
- fsLock , err := lockfile .New (filepath .Join (dir , "tf-apply.lck" ))
102
- if err != nil {
103
- panic (err )
104
- }
105
100
106
101
var pluginLookup func () discovery.Plugins
107
102
if ! standalone {
108
- if err = local .Setup (); err != nil {
103
+ if err : = local .Setup (); err != nil {
109
104
panic (err )
110
105
}
111
106
plugins , err := local .NewPluginDiscovery ()
@@ -119,7 +114,6 @@ func NewTerraformInstancePlugin(dir string, pollInterval time.Duration, standalo
119
114
p := plugin {
120
115
Dir : dir ,
121
116
fs : afero .NewOsFs (),
122
- fsLock : fsLock ,
123
117
pollInterval : pollInterval ,
124
118
pluginLookup : pluginLookup ,
125
119
envs : envs ,
@@ -952,16 +946,9 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
952
946
// we simply look for vm instance and merge in the tags, and user init, etc.
953
947
954
948
// Hold the fs lock for the duration since the file is written at the end
955
- var name string
956
- for {
957
- if err := p .fsLock .TryLock (); err == nil {
958
- defer p .fsLock .Unlock ()
959
- name = ensureUniqueFile (p .Dir )
960
- break
961
- }
962
- log .Infoln ("Can't acquire fsLock on Provision, waiting" )
963
- time .Sleep (time .Second )
964
- }
949
+ p .fsLock .Lock ()
950
+ defer p .fsLock .Unlock ()
951
+ name := ensureUniqueFile (p .Dir )
965
952
id := instance .ID (name )
966
953
967
954
// Decode the given spec and find the VM resource
@@ -1003,14 +990,8 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
1003
990
1004
991
// Label labels the instance
1005
992
func (p * plugin ) Label (instance instance.ID , labels map [string ]string ) error {
1006
- // Acquire lock
1007
- for {
1008
- if err := p .fsLock .TryLock (); err == nil {
1009
- defer p .fsLock .Unlock ()
1010
- break
1011
- }
1012
- time .Sleep (time .Second )
1013
- }
993
+ p .fsLock .Lock ()
994
+ defer p .fsLock .Unlock ()
1014
995
1015
996
tf , filename , err := p .parseFileForInstanceID (instance )
1016
997
if err != nil {
@@ -1041,27 +1022,21 @@ func (p *plugin) Label(instance instance.ID, labels map[string]string) error {
1041
1022
1042
1023
// Destroy terminates an existing instance.
1043
1024
func (p * plugin ) Destroy (instID instance.ID , context instance.Context ) error {
1025
+ // Acquire Lock outside of recursive doDestroy function
1026
+ p .fsLock .Lock ()
1027
+ defer p .fsLock .Unlock ()
1028
+
1044
1029
processAttach := true
1045
1030
if context == instance .RollingUpdate {
1046
1031
// Do not destroy related resources since this instance will be re-provisioned
1047
1032
processAttach = false
1048
1033
}
1049
- err := p .doDestroy (instID , processAttach , true )
1050
- return err
1034
+ return p .doDestroy (instID , processAttach , true )
1051
1035
}
1052
1036
1053
1037
// doDestroy terminates an existing instance and optionally terminates any related
1054
1038
// resources
1055
1039
func (p * plugin ) doDestroy (inst instance.ID , processAttach , executeTfApply bool ) error {
1056
- // Acquire lock
1057
- for {
1058
- if err := p .fsLock .TryLock (); err == nil {
1059
- defer p .fsLock .Unlock ()
1060
- break
1061
- }
1062
- time .Sleep (time .Second )
1063
- }
1064
-
1065
1040
tf , filename , err := p .parseFileForInstanceID (inst )
1066
1041
if err != nil {
1067
1042
return err
@@ -1159,13 +1134,8 @@ func parseAttachTag(tf *TFormat) ([]string, error) {
1159
1134
func (p * plugin ) DescribeInstances (tags map [string ]string , properties bool ) ([]instance.Description , error ) {
1160
1135
log .Debugln ("describe-instances" , tags )
1161
1136
// Acquire lock since we are reading all files and potentially running "terraform show"
1162
- for {
1163
- if err := p .fsLock .TryLock (); err == nil {
1164
- defer p .fsLock .Unlock ()
1165
- break
1166
- }
1167
- time .Sleep (time .Second )
1168
- }
1137
+ p .fsLock .Lock ()
1138
+ defer p .fsLock .Unlock ()
1169
1139
1170
1140
// localSpecs are what we told terraform to create - these are the generated files.
1171
1141
localSpecs , err := p .scanLocalFiles ()
@@ -1309,16 +1279,9 @@ type importFns struct {
1309
1279
// .tf.json.new file based on the given spec
1310
1280
func (p * plugin ) importResources (fns importFns , resources []* ImportResource , spec * instance.Spec ) error {
1311
1281
// Acquire lock since we are creating a tf.json.new file and updating terraform state
1312
- var vmResName string
1313
- for {
1314
- if err := p .fsLock .TryLock (); err == nil {
1315
- defer p .fsLock .Unlock ()
1316
- vmResName = ensureUniqueFile (p .Dir )
1317
- break
1318
- }
1319
- log .Infoln ("Can't acquire fsLock on importResource, waiting" )
1320
- time .Sleep (time .Second )
1321
- }
1282
+ p .fsLock .Lock ()
1283
+ defer p .fsLock .Unlock ()
1284
+ vmResName := ensureUniqueFile (p .Dir )
1322
1285
1323
1286
// Parse the instance spec
1324
1287
tf := TFormat {}
0 commit comments