| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.compute.callables; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.util.concurrent.atomic.AtomicInteger; |
| 25 | |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.scriptbuilder.InitBuilder; |
| 30 | |
| 31 | import com.google.common.base.Supplier; |
| 32 | import com.google.inject.Inject; |
| 33 | |
| 34 | /** |
| 35 | * |
| 36 | * @author Adrian Cole |
| 37 | */ |
| 38 | @Singleton |
| 39 | public class InitScriptConfigurationForTasks { |
| 40 | public static final String PROPERTY_INIT_SCRIPT_PATTERN = "jclouds.compute.init-script-pattern"; |
| 41 | |
| 42 | public static InitScriptConfigurationForTasks create() { |
| 43 | return new InitScriptConfigurationForTasks(); |
| 44 | } |
| 45 | |
| 46 | private String basedir = "/tmp"; |
| 47 | private String initScriptPattern = basedir + "/init-%s"; |
| 48 | private Supplier<String> suffixSupplier; |
| 49 | |
| 50 | protected InitScriptConfigurationForTasks() { |
| 51 | appendCurrentTimeMillisToAnonymousTaskNames(); |
| 52 | } |
| 53 | |
| 54 | @Inject(optional = true) |
| 55 | public InitScriptConfigurationForTasks initScriptPattern( |
| 56 | @Named(PROPERTY_INIT_SCRIPT_PATTERN) String initScriptPattern) { |
| 57 | this.initScriptPattern = checkNotNull(initScriptPattern, "initScriptPattern ex. /tmp/init-%s"); |
| 58 | this.basedir = new File(initScriptPattern).getParent(); |
| 59 | return this; |
| 60 | } |
| 61 | |
| 62 | public InitScriptConfigurationForTasks appendCurrentTimeMillisToAnonymousTaskNames() { |
| 63 | this.suffixSupplier = new Supplier<String>() { |
| 64 | |
| 65 | @Override |
| 66 | public String get() { |
| 67 | return System.currentTimeMillis() + ""; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | return "currentTimeMillis()"; |
| 73 | } |
| 74 | }; |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | public InitScriptConfigurationForTasks appendIncrementingNumberToAnonymousTaskNames() { |
| 79 | this.suffixSupplier = new Supplier<String>() { |
| 80 | private final AtomicInteger integer = new AtomicInteger(); |
| 81 | |
| 82 | @Override |
| 83 | public String get() { |
| 84 | return integer.getAndIncrement() + ""; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String toString() { |
| 89 | return "incrementingNumber()"; |
| 90 | } |
| 91 | }; |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Directory where the init script is stored. the runtime directory of the process will be in |
| 97 | * this dir/taskName |
| 98 | */ |
| 99 | public String getBasedir() { |
| 100 | return basedir; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * |
| 105 | * @return the naming convention of init scripts. ex. {@code /tmp/init-%s}, noting logs are under |
| 106 | * the basedir/%s where %s is the taskName |
| 107 | * @see InitBuilder#getHomeDir |
| 108 | * @see InitBuilder#getLogDir |
| 109 | */ |
| 110 | public String getInitScriptPattern() { |
| 111 | return initScriptPattern; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return suffix where the taskName isn't set. by default this is |
| 116 | * {@link System#currentTimeMillis} |
| 117 | * @see #appendCurrentTimeMillisToAnonymousTaskNames |
| 118 | * @see #appendIncrementingNumberToAnonymousTaskNames |
| 119 | */ |
| 120 | public Supplier<String> getAnonymousTaskSuffixSupplier() { |
| 121 | return suffixSupplier; |
| 122 | } |
| 123 | } |