Browse Source
- Checking for kubectl - Checking for a cluster - More in depth docker validationdavidfowl/dependencies
15 changed files with 171 additions and 8 deletions
@ -0,0 +1,52 @@ |
|||
// Licensed to the .NET Foundation under one or more agreements.
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
|||
// See the LICENSE file in the project root for more information.
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Tye |
|||
{ |
|||
internal class DockerDetector |
|||
{ |
|||
public static DockerDetector Instance { get; } = new DockerDetector(); |
|||
|
|||
private DockerDetector() |
|||
{ |
|||
IsDockerInstalled = new Lazy<Task<bool>>(DetectDockerInstalled); |
|||
IsDockerConnectedToDaemon = new Lazy<Task<bool>>(DetectDockerConnectedToDaemon); |
|||
} |
|||
|
|||
public Lazy<Task<bool>> IsDockerInstalled { get; } |
|||
|
|||
public Lazy<Task<bool>> IsDockerConnectedToDaemon { get; } |
|||
|
|||
private async Task<bool> DetectDockerInstalled() |
|||
{ |
|||
try |
|||
{ |
|||
await ProcessUtil.RunAsync("docker", "version", throwOnError: false); |
|||
return true; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
// Unfortunately, process throws
|
|||
return false; |
|||
} |
|||
} |
|||
|
|||
private async Task<bool> DetectDockerConnectedToDaemon() |
|||
{ |
|||
try |
|||
{ |
|||
var result = await ProcessUtil.RunAsync("docker", "version", throwOnError: false); |
|||
return result.ExitCode == 0; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
// Unfortunately, process throws
|
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
// Licensed to the .NET Foundation under one or more agreements.
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
|||
// See the LICENSE file in the project root for more information.
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Tye |
|||
{ |
|||
internal class KubectlDetector |
|||
{ |
|||
public static KubectlDetector Instance { get; } = new KubectlDetector(); |
|||
|
|||
private KubectlDetector() |
|||
{ |
|||
IsKubectlInstalled = new Lazy<Task<bool>>(DetectKubectlInstalled); |
|||
IsKubectlConnectedToCluster = new Lazy<Task<bool>>(DetectKubectlConnectedToCluster); |
|||
} |
|||
|
|||
public Lazy<Task<bool>> IsKubectlInstalled { get; } |
|||
|
|||
public Lazy<Task<bool>> IsKubectlConnectedToCluster { get; } |
|||
|
|||
private static async Task<bool> DetectKubectlInstalled() |
|||
{ |
|||
try |
|||
{ |
|||
// Ignoring the exit code and relying on Process to throw if kubectl is not found
|
|||
// kubectl version will return non-zero if you're not connected to a cluster.
|
|||
await ProcessUtil.RunAsync("kubectl", "version", throwOnError: false); |
|||
return true; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
// Unfortunately, process throws
|
|||
return false; |
|||
} |
|||
} |
|||
|
|||
private static async Task<bool> DetectKubectlConnectedToCluster() |
|||
{ |
|||
try |
|||
{ |
|||
var result = await ProcessUtil.RunAsync("kubectl", "cluster-info", throwOnError: false); |
|||
return result.ExitCode == 0; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
// Unfortunately, process throws
|
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue